Check for Lexicographically Bigger Permutation Between Two Strings in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:53:00

152 Views

Suppose we have two strings s and t of the same size, we have to check whether there is some permutation of s, say s1, and permutation of t, say t1, such that: s1[i] ≤ t1[i] for all 0 ≤ i < n or t1[i] ≤ s1[i] for all 0 ≤ i < n.So, if the input is like s = "vyx" t = "wzx", then the output will be True, as we can have s1 = "vxy" and t1 = "wxz".To solve this, we will follow these steps −if s and t are empty, thenreturn Trues := sort the ... Read More

Minimum Number of Bricks to Make K Towers of Same Height in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:51:50

556 Views

Suppose we have a list of tower heights, and a positive value k. We want to select k towers and make them all the same height by adding more bricks, but using as few bricks as possible. We have to find the minimum number of bricks are needed to pick k towers and make them the same height.So, if the input is like heights = [4, 7, 31, 14, 40] k = 3, then the output will be 17, as we can select 5, 8, and 15 which requires 17 bricks to make the same height.To solve this, we will ... Read More

Find Sum of Non-Adjacent Elements in a Circular List in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:49:49

340 Views

Suppose we have a list of numbers called nums that is representing a circular list. We have to find the largest sum of non-adjacent numbers.So, if the input is like nums = [10, 3, 4, 8], then the output will be 14, as we can take 10 and 4. We cannot take 10 and 8 as they are adjacent.To solve this, we will follow these steps −n := size of numsnums1 := nums[from index 0 to n - 2]nums2 := nums[from index 1 to end]Define a function f() . This will take iif i >= size of nums1 , thenreturn ... Read More

Find the Sum of Largest K Sublist in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:48:55

296 Views

Suppose we have a list of numbers called nums, and another value k, which represents a large list of nums concatenated k times. We have to find the sum of the contiguous sublist with the largest sum.So, if the input is like nums = [1, 3, 4, -5], k = 1, then the output will be 11, as we can take the sublist like [2, 4, 5]To solve this, we will follow these steps −s := ans := lo := 0for all value in range 0 to minimum of k and 2, dofor each x in nums, dos := s ... Read More

Find Area of Largest Island in a Matrix in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:44:56

414 Views

Suppose we have a binary matrix. Here 1 represents land and 0 represents water, And an island is a group of 1s that are neighboring whose perimeter is surrounded by water. We can assume that the edges of the matrix are surrounded by water. We have to find the area of the largest island in matrix.So, if the input is like001111100000000111100001100000000110000010then the output will be 6.To solve this, we will follow these steps −Define a function dfs() . This will take matrix, r, ctotal := total + 1matrix[r, c] := 0if r - 1 >= 0 and matrix[r - 1, ... Read More

Find Largest Distance Pair from Two Lists of Numbers in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:43:02

183 Views

Suppose we have two list of numbers called A and B, and their lengths are same. We have to find the maximum value for all 0 ≤ i < j < n: |a[i] - a[j]| + |b[i] - b[j]| + |i - j|So, if the input is like A = [2, 4, 10, 6] B = [3, 4, 7, 5], then the output will be 14, as when i = 0 and j = 2 and we get |2 - 10| + |3 - 7| + |1 - 3|.To solve this, we will follow these steps −ans := 0n := ... Read More

Find Difference Between Node and Descendant in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:41:49

158 Views

Suppose we have a binary tree, we have to find the largest absolute difference between any node and its descendants.So, if the input is likethen the output will be 7 as largest absolute difference is between nodes 8 and 1.To solve this, we will follow these steps −Define a function dfs() . This will take nodeif node is not null, thenreturn a list with positive and negative infinityleft := dfs(left of node)right := dfs(right of node)res := a pair with (minimum of left[0], right[0] and value of node, and maximum of left[1], right[1] and value of node)ans := maximum of ... Read More

Find Percentage of Knight's Move Positions in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:39:14

213 Views

Suppose we have four values n, x, y, and k. Here n indicates an n x n chessboard and x, y coordinate represents a knight is placed at (x, y). The knight has to take exactly k steps, where at each step it can move any of the 8 directions uniformly at random. We have to find the percentage chance (nearest integer) that the knight remains in the chessboard after taking k moves. We have to follow a condition that it cannot enter the board again once it leaves it.So, if the input is like n = 8, (x = ... Read More

Minimum Steps to Reach Target Position by Chess Knight in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:37:29

975 Views

Suppose we have two values r and c. If a chess knight is placed at the coordinate (0, 0) at the beginning in an infinitely large chess board, we have to find minimum number of moves it would take to reach the location (r, c). The knight will follow same moving style as chess. It moves two squares away horizontally and one square vertically, or two squares vertically and one square horizontally.So, if the input is like r = 6, c = 1, then the output will be 3, the red is initial position, green is final and yellows are ... Read More

Find K Sublists with Largest Sums in Ascending Order in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:36:22

186 Views

Suppose we have a list of numbers called nums, and another value k, we have to find k sublists with the largest sums and return the sums in non-decreasing order.So, if the input is like nums = [2, 4, 5, -100, 12, -30, 6, -2, 6] k = 3, then the output will be [10, 11, 12], as we have these 3 sublists with the largest sums − [6, -2, 6], [2, 4, 5], [12].To solve this, we will follow these steps −ps := a list of 1 + size of nums and fill with 0for each index i and ... Read More

Advertisements