Find Minimum Cost to Reduce a List to One Integer in Python

Arnab Chakraborty
Updated on 19-Oct-2020 16:14:56

807 Views

Suppose we have a list of numbers called nums. We can reduce the length of nums by taking any two numbers, removing them, and appending their sum at the end. The cost of doing this operation is the sum of the two integers we removed. We have to find the minimum total cost of reducing nums to one integer.So, if the input is like nums = [2, 3, 4, 5, 6], then the output will be 45, as we take 2 and 3 then remove to get [4, 5, 6, 5], then we take 4 and 5 then remove to ... Read More

Check If Two Strings Are 0 or 1 Edit Distance Away in Python

Arnab Chakraborty
Updated on 19-Oct-2020 16:10:51

428 Views

Suppose we have two strings S and T we have to check whether they are one or zero edit distance away or not. An edit operation can be defined as deleting a character, adding a character, or replacing a character with another character.So, if the input is like S = "hello", T = "hallo", then the output will be True, as these two strings have edit distance of 1.To solve this, we will follow these steps −m := size of S, n := size of Ti := 0, j := 0count := 0if |m - n| > 1, thenreturn Falsewhile ... Read More

Product of All Elements Except Current Index in Python

Arnab Chakraborty
Updated on 19-Oct-2020 16:07:20

692 Views

Suppose we have a list of numbers called nums, we have to find a new list such that each element at index i of the newly generated list is the product of all the numbers in the original list except the one at index i. Here we have to solve it without using division.So, if the input is like nums = [2, 3, 4, 5, 6], then the output will be [360, 240, 180, 144, 120]To solve this, we will follow these steps −if size of nums < 1, thenreturn numsl := size of numsleft := a list of size ... Read More

Check Palindromic Substrings for Odd Length in Python

Arnab Chakraborty
Updated on 19-Oct-2020 16:05:05

303 Views

Suppose we have a string s, we have to check whether all its palindromic substrings have odd lengths or not.So, if the input is like s = "level", then the output will be TrueTo solve this, we will follow these steps −for i in range 1 to size of s, doif s[i] is same as s[i - 1], thenreturn Falsereturn TrueLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, s):       for i in range(1, len(s)):          if s[i] == s[i - 1]:           ... Read More

Minimum Number of Hops to Reach End Position in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:53:40

630 Views

Suppose we have one array nums, where all elements are positive. We are at index 0. Here, each element in the array represents our maximum jump length at that position. Our goal is to reach to the final index (n-1, where n is size of nums) with less number of jumps. So if the array is like [2, 3, 1, 1, 4], and then the output will be 2, as we can jump to index 1 from 0, then jump to index 4, that is the last index.To solve this, we will follow these steps −end := 0, jumps := ... Read More

Check if Everyone Has at Least One Friend in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:48:52

340 Views

Suppose we have n people represented as a number from 0 to n - 1, we also have a list of friend’s tuples, where friends[i][0] and friends[i][1] are friends. We have to check whether everyone has at least one friend or not.So, if the input is like n = 3 friends = [ [0, 1], [1, 2] ], then the output will be True, as Person 0 is friends of Person 1, Person 1 is friends of Person 0 and 2, and Person 2 is friends of Person 1.To solve this, we will follow these steps −people := a list ... Read More

Find Most Frequent Subtree Sum of a Binary Tree in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:45:49

203 Views

Suppose we have a binary tree, we have to find the most frequent subtree sum. The subtree sum of a node is actually the sum of all values under a node, including the node itself.So, if the input is likethen the output will be 3 as it occurs twice − once as the left leaf, and once as the sum of 3 - 6 + 6.To solve this, we will follow these steps −count := an empty mapDefine a function getSum() . This will take nodeif node is null, thenreturn 0mySum := getSum(left of node) + getSum(right of node) + ... Read More

Count Non-Empty Subsets in Python Where Sum of Min and Max is Less Than K

Arnab Chakraborty
Updated on 19-Oct-2020 15:42:36

244 Views

Suppose we have a list of numbers called nums and another value k, we have to find the number of non-empty subsets S such that min of S + max of S K, doj := j - 1if i

Find Maximum Time to Finish K Tasks in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:38:54

380 Views

Suppose we have a matrix of tasks where each row has 3 values. We also have another value k. We have to select k rows from tasks, call it S, such that the following sum is minimized and return the sum as: maximum of (S[0, 0], S[1, 0], ...S[k - 1, 0]) + maximum of (S[0, 1], S[1, 1], ...S[k - 1, 1]) + maximum of (S[0, 2], S[1, 2], ...S[k - 1, 2]) We can also say like: Each of the 3 columns contribute to a cost, and is calculated by taking the max value of that column in ... Read More

Find Minimum Difference of Max and Mins After Updating Elements in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:37:01

217 Views

Suppose we have a list of numbers called nums, now consider an operation where we can update an element to any value. We can perform at most 3 of such operations, we have to find the resulting minimum difference between the max and the min value in nums.So, if the input is like nums = [2, 3, 4, 5, 6, 7], then the output will be 2, as we can change the list to [4, 3, 4, 5, 4, 4] and then 5 - 3 = 2.To solve this, we will follow these steps −if size of nums

Advertisements