Found 10476 Articles for Python

Partition Array for Maximum Sum in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:06:21

1K+ Views

Suppose we have an integer array A, we have to partition the array into (contiguous) subarrays of length at most K. After partitioning, each subarray has their values changed to become the maximum value of that subarray. We have to find the largest sum of the given array after partitioning. So if input is like [1, 15, 7, 9, 2, 5, 10] and k = 3, then the output will be 84. This is because the array becomes [15, 15, 15, 9, 10, 10, 10]To solve this, we will follow these steps −make one array dp of length same as ... Read More

Smallest Integer Divisible by K in Python

Arnab Chakraborty
Updated on 30-Apr-2020 07:52:31

334 Views

Suppose we have a positive integer K, we need find the smallest positive integer N such that N is divisible by K, and N only contains the digit 1. We have to find the length of N. If there is no such N, return -1. So if the input is like 3, then the output will be 3. The smallest answer will be N = 111.To solve this, we will follow these steps −if k is even, or k is divisible by 5, then return -1set r := 0 and N = 1for i in range 1 to K + ... Read More

Clumsy Factorial in Python

Arnab Chakraborty
Updated on 30-Apr-2020 07:49:18

2K+ Views

As we know that the factorial of a positive integer n is the product of all positive integers less than or equal to n. So factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We will try to find a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order.The clumsy factorial is like clumsy(10) = 10 * 9 / 8 + 7 - 6 * ... Read More

Minimum Add to Make Parentheses Valid in Python

Arnab Chakraborty
Updated on 30-Apr-2020 07:38:19

401 Views

Suppose we have a string S of '(' and ')' parentheses, we add the minimum number of parentheses at any positions, so that the resulting parentheses string is valid. A parentheses string is valid if and only if −It is the empty stringIt can be written as XY (X concatenated with Y), where X and Y are valid stringsIt can be written as (A), where A is a valid string.So if the string is like "()))((", then we need to add 4 more parentheses to make the string valid.To solve this, we will follow these steps −if S is empty, ... Read More

Find and Replace Pattern in Python

Arnab Chakraborty
Updated on 30-Apr-2020 07:29:54

188 Views

Suppose we have a list of words and a pattern, and we have to find which words in words matches the pattern. Here a word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the target word. We have to find a list of the words in words that match the given pattern.So for example, if the input is like ["abc", "deq", "mee", "aqq", "dkd", "ccc"] and pattern is “abb”, then the output will be [“mee”, “aqq”], here mee and aqq are matching the ... Read More

Construct Binary Tree from Preorder and Postorder Traversal in Python

Arnab Chakraborty
Updated on 30-Apr-2020 06:14:19

479 Views

Suppose we have two traversal sequences Preorder and Postorder, we have to generate the binary tree from these two sequences. So if the sequences are [1, 2, 4, 5, 3, 6, 7], [4, 5, 2, 6, 7, 3, 1], then the output will beTo solve this, we will follow these steps −ans := make a tree node by taking value pre[0], stack := empty stack, and insert ansi := 1 and j := 0while i < length of pre and j < length of postif stack top value = post[j], then increase j by 1, pop from stack, and go ... Read More

Decoded String at Index in Python

Arnab Chakraborty
Updated on 30-Apr-2020 06:09:03

200 Views

Suppose one encoded string S is given. We have to find and write the decoded string to a tape, here the encoded string is read one character at a time and the following steps are performed −If the character read is a letter, that letter is simply written onto the tape.If the character read is a digit, the entire current tape is repeatedly written digit – 1 more times in total.Now if some encoded string S, and an index K is given, find and return the K-th letter (starting indices from 1) in the decoded string.So if the string is ... Read More

Kth Smallest Element in a Sorted Matrix in Python

Arnab Chakraborty
Updated on 30-Apr-2020 05:54:09

448 Views

Suppose we have a n x n matrix where each of the rows and columns are sorted in increasing order, we have to find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth unique element. So if the input is like [[1, 5, 9], [10, 11, 13], [12, 13, 15]], if k = 8, then the output will be 13.To solve this, we will follow these steps −define one method called checkVal() and the arguments are matrix and valuei := 0, j := length of matrix[0] – ... Read More

Surrounded Regions in Python

Arnab Chakraborty
Updated on 04-May-2020 06:35:05

311 Views

Suppose we have a 2D board containing X and O. Capture all regions surrounded by X. A region is captured by changing all Os into Xs in that surrounded region.XXXXXOOXXXOXXOXXAfter running the output will beXXXXXXXXXXXXXOXXTo solve this, we will follow these steps −If board is not present, then return blank boardfor i in range 0 to number of rows – 1 −if board[i, 0] = ‘O’, then make_one(board, i, 0)if board[i, length of row - 1] = ‘O’, then make_one(board, i, length of row – 1)for i in range 0 to number of cols – 1 −if board[0, i] = ... Read More

Search in Rotated Sorted Array II in Python

Arnab Chakraborty
Updated on 04-May-2020 06:19:34

309 Views

Consider we have an array sorted in ascending order. That is rotated at some pivot unknown to us beforehand. For example, if the array is like [0, 0, 1, 2, 2, 5, 6], this might become [2, 5, 6, 0, 0, 1, 2]. We have a target value to search. If that is found in the array, then return true, otherwise return false. So if the array is like [2, 5, 6, 0, 0, 1, 2], and target is 0, then the output will be 0Let us see the steps −low := 0 and high := size of arraywhile low ... Read More

Advertisements