Minimum Cost to Reach Final Index with At Most K Steps in Python

Arnab Chakraborty
Updated on 02-Dec-2020 05:50:04

260 Views

Suppose we have a list of numbers nums and another value k. Here the items at nums[i] represents the costs of landing at index i. If we start from index 0 and end at the last index of nums. In each step we can jump from some position X to any position up to k steps away. We have to minimize the sum of costs to reach last index, so what will be the minimum sum?So, if the input is like nums = [2, 3, 4, 5, 6] k = 2, then the output will be 12, as we can ... Read More

Find Maximum Profit After Buying and Selling Stocks Twice in Python

Arnab Chakraborty
Updated on 02-Dec-2020 05:48:17

294 Views

Suppose we have a list of numbers called prices and that is representing the stock prices of a company in chronological order, we have to find the maximum profit we can make from buying and selling that stock at most two times. We have to buy first then sell.So, if the input is like prices = [2, 6, 3, 4, 2, 9], then the output will be 11, as we can buy at price 2, then sell at 6, again buy at 2, and sell at 9.To solve this, we will follow these steps −first_buy := -inf, first_sell := -infsecond_buy ... Read More

Check If String Can Be Broken Into Given List of Words in Python

Arnab Chakraborty
Updated on 02-Dec-2020 05:47:04

609 Views

Suppose we have a word list and another string s with no spaces. We have to check whether the string can be broken down using the list of words or not.So, if the input is like words = ["love", "python", "we", "programming", "language"] s = "welovepythonprogramming", then the output will be TrueTo solve this, we will follow these steps −words := a new set of all unique wordsDefine a function rec() . This will take iif i is same as size of s , thenreturn Trueacc := blank stringfor j in range i to size of s, doacc := acc ... Read More

Find Maximum Number of Boxes Fit Inside Another Box in Python

Arnab Chakraborty
Updated on 02-Dec-2020 05:45:08

1K+ Views

Suppose we have a list of boxes where each row represents the height and width of given boxes. We can put a box in another box if first box is smaller than the second one (when both of its width and height are smaller than the other box), we have to find the maximum number of boxes we can fit into a box.So, if the input is likeWidthHeight1212101066510then the output will be 3, as we can fit the box [6, 6] inside [10, 10] which we can be put into [12, 12] box.To solve this, we will follow these steps ... Read More

Find Length of Longest Consecutive Path in a Binary Tree using Python

Arnab Chakraborty
Updated on 02-Dec-2020 05:43:20

255 Views

Suppose we have a binary tree; we have to find the longest path in the binary tree.So, if the input is likethen the output will be 5 as longest consecutive sequence is [2, 3, 4, 5, 6].To solve this, we will follow these steps −if root is null, thenreturn 0maxPath := 0Define a function helper() . This will take nodeinc := 1, dec := 1if left of node is not null, then[left_inc, left_dec] := helper(left of node)otherwise, [left_inc, left_dec] := [0, 0]if right of node is not null, then[right_inc, right_dec] := helper(right of node)otherwise, [right_inc, right_dec] := [0, 0]if left ... Read More

Make Almost BST to Exact BST in Python

Arnab Chakraborty
Updated on 02-Dec-2020 05:40:59

193 Views

Suppose we have a binary tree and that is almost a binary search tree. Only two nodes' value are swapped. We have to correct it and return the binary search tree.So, if the input is likethen the output will beTo solve this, we will follow these steps −prev_node := null, min_node := null, max_node := nullfound_one := Falsefor each node in the inorder traversal of root, doif prev_node is not null, thenif value of node < value of prev_node, thenif min_node is null or value of node < value of min_node, thenmin_node := nodeif max_node is null or value of ... Read More

Find Area of Largest Square of 1s in a Given Matrix in Python

Arnab Chakraborty
Updated on 02-Dec-2020 05:38:26

735 Views

Suppose we have a binary matrix, we have to find largest square of 1s in that given matrix.So, if the input is like100001100000110111100011110001111000111100then the output will be 16.To solve this, we will follow these steps −res := 0for i in range 0 to size of matrix, dores := maximum of res and matrix[i, 0]for i in range 0 to size of matrix[0], dores := maximum of res and matrix[0, i]for i in range 1 to row count of matrix, dofor j in range 1 to column count of matrix, doif matrix[i, j] is same as 1, thenmatrix[i, j] = minimum ... Read More

Find Largest Rectangle Area Under Histogram in Python

Arnab Chakraborty
Updated on 02-Dec-2020 05:36:52

1K+ Views

Suppose we have a list of numbers representing heights of bars in a histogram. We have to find area of the largest rectangle that can be formed under the bars.So, if the input is like nums = [3, 2, 5, 7]then the output will be 10To solve this, we will follow these steps −stk := a stack and initially insert -1 into itinsert 0 at the end of heightsans := 0for i in range 0 to size of heights, dowhile heights[i] < heights[top of stk], doh := heights[top of stk] and pop from stkw := i - top of stk ... Read More

Count Numbers to Create All from 1 to K in C++

Arnab Chakraborty
Updated on 02-Dec-2020 05:34:56

100 Views

Suppose we have a list of numbers called nums and another value k. We have to find the minimum number of numbers that we need to insert into nums such that we can make any number from [1, k] using some subset in nums.So, if the input is like nums = [3, 5], k = 6, then the output will be 2, as we have to insert 1, 2, so we can make : 1 = [1], 2 = [2], 3 = [3], 4 = [1, 3], 5 = [5], 6 = [1, 5].To solve this, we will follow these ... Read More

Find Minimum Swaps Required to Make Given Anagram in Python

Arnab Chakraborty
Updated on 02-Dec-2020 05:33:15

386 Views

Suppose we have two strings S and T and they are anagrams of each other. We have to find the minimum number of swaps required in S to make it same as T.So, if the input is like S = "kolkata" T = "katloka", then the output will be 3, as can swap in this sequence [katloka (given), kotlaka, koltaka, kolkata].To solve this, we will follow these steps −Define a function util() . This will take S, T, iif i >= size of S , thenreturn 0if S[i] is same as T[i], thenreturn util(S, T, i + 1)x := T[i]ret ... Read More

Advertisements