Found 10476 Articles for Python

Program to find maximum additive score by deleting numbers in Python

Arnab Chakraborty
Updated on 20-Nov-2020 05:50:07

183 Views

Suppose we have a list of numbers called nums. Let us consider an operation where we can select a number, then remove it and increase our score by the sum of the number and its two adjacent numbers. If we can perform this operation as many times as we want as long as we do not select the first or the last number in the list. We have to find the maximal score possible.So, if the input is like nums = [2, 3, 4, 5, 6], then the output will be 39, as we can select 5, then sum will ... Read More

Program to find a target value inside given matrix or not in Python

Arnab Chakraborty
Updated on 20-Nov-2020 05:48:13

439 Views

Suppose we have a 2D matrix, where each row and column is sorted in non-decreasing order, we have to check whether given target is present inside it or not.So, if the input is like243034316632And target = 31, then the output will be TrueTo solve this, we will follow these steps −col := column size of matrix - 1for i in range 0 to row size of matrix, dowhile matrix[i, col] > target and col >= 0, docol := col - 1if matrix[i, col] is same as target, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Example Live ... Read More

Program to find nth smallest number from a given matrix in Python

Arnab Chakraborty
Updated on 20-Nov-2020 05:47:00

175 Views

Suppose we have a 2D matrix, where each row and column is sorted in non-decreasing order, we have to find the nth smallest number.So, if the input is like243034316632And n = 4, then the output will be 6.To solve this, we will follow these steps −lst := a new listfor each row i in matrix, dofor each cell j in i, doinsert j at the end of lstsort the list lstreturn lst[n]Let us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, matrix, n):       lst = []       for i ... Read More

Program to generate matrix where each cell holds Manhattan distance from nearest 0 in Python

Arnab Chakraborty
Updated on 20-Nov-2020 05:45:39

233 Views

Suppose we have a binary matrix. We have to find the same matrix, but each cell's value will be the Manhattan distance to the nearest 0. We can assume at least one 0 exists in the matrix.So, if the input is like101101110then the output will be101101210as only the bottom left cell has distance of 2 to the nearest 0.To solve this, we will follow these steps −m := row size of matrix, n := column size of matrixfor y in range 0 to m, dofor x in range 0 to n, doif matrix[y, x] is non-zero, thenmatrix[y, x] := infinityfor ... Read More

Program to find number of combinations of coins to reach target in Python

Arnab Chakraborty
Updated on 20-Nov-2020 05:43:36

1K+ Views

Suppose we have a list of coins and another value amount, we have to find the number of combinations there are that sum to amount. If the answer is very large, then mod the result by 10^9 + 7.So, if the input is like coins = [2, 5] amount = 10, then the output will be 2, as we can make these combinations − [2, 2, 2, 2, 2], [5, 5]To solve this, we will follow these steps −m := 10^9 + 7dp := a list of size same as amount + 1, and fill it with 0dp[0] := 1for ... Read More

Program to find lowest sum of pairs greater than given target in Python

Arnab Chakraborty
Updated on 20-Nov-2020 05:40:22

256 Views

Suppose we have a list of numbers called nums and another value target. We have to find the lowest sum of pair of numbers that is larger than target.So, if the input is like nums = [2, 4, 6, 10, 14] target = 10, then the output will be 12, as we pick 2 and 10To solve this, we will follow these steps −sort the list numsn := size of numsanswer := 10^10i := 0, j := n - 1while i < j, doif nums[i] + nums[j] > target, thenanswer := minimum of answer and (nums[i] + nums[j])j := j ... Read More

Nested List Weight Sum II in Python

Arnab Chakraborty
Updated on 19-Nov-2020 09:43:00

294 Views

Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list -- whose elements may also be integers or other lists. Different from the previous question where weight is increasing from root to leaf, now the weight is defined from bottom up. i.e., the leaf level integers have weight 1, and the root level integers have the largest weight.So, if the input is like [[1, 1], 2, [1, 1]], then the output will be 8, as four 1's at depth 1, one 2 ... Read More

Program to find nth term in Look and Say Sequence in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:28:45

634 Views

Suppose we have a number n we have to generate nth term in “Look and Say” sequence. This is a sequence whose few terms are like below −111211211111221The string will be read like1 (One)11 (One 1) So read the previous 1, and say “One 1”21 (Two 1) So read the previous 11, and say “Two 1”1211 (One 2 one 1) So read the previous 21, and say “One 2 one 1”111221 (One 1 one 2 two 1) So read the previous 1211, and say “One 1 one 2 two 1”Suppose we have a number n, 1

Program to find length of longest substring with character count of at least k in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:26:55

252 Views

Suppose we have a string s where each characters are sorted and we also have a number k, we have to find the length of the longest substring such that every character occurs at least k times.So, if the input is like s = "aabccddeeffghij" k = 2, then the output will be 8, as the longest substring here is "ccddeeff" here every character occurs at least 2 times.To solve this, we will follow these steps −Define a function rc() . This will take lstc := a map with all characters and their occurrencesacc := a new listans := 0valid ... Read More

Program to find length of longest sublist where difference between min and max smaller than k in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:24:34

224 Views

Suppose we have a list of numbers called nums and another value k, we have to find the length of longest sublist where the absolute difference between the largest and smallest element is ≤ k.So, if the input is like nums = [2, 4, 6, 10] k = 4, then the output will be 3, as we can select pick [2, 4, 6] here the absolute difference is 4.To solve this, we will follow these steps −Create two double ended queue maxd, mindi := 0, res := 1for each index j and value a in A, dowhile maxd is not ... Read More

Advertisements