Found 10476 Articles for Python

Broken Calculator in Python

Arnab Chakraborty
Updated on 30-Apr-2020 10:51:35

359 Views

Suppose we have a broken calculator that has a number showing on its display, we can perform only two operations −Double − This will multiply the number on the display by 2, or;Decrement − This will decrease the number that is showing, by 1, Initially, the calculator is displaying the number X. We have to find the minimum number of operations needed to display the number Y.So if the input is like X = 5 and Y is 8, then the output will be 2, as decrement, it once, then double itTo solve this, we will follow these steps −res ... Read More

Insert Delete GetRandom O(1) in Python

Arnab Chakraborty
Updated on 29-Apr-2020 12:34:50

563 Views

Suppose we have a data structure that supports all following operations in average O(1) time.insert(val) − This will Insert an item val to the set if that is not already present.remove(val) − This will remove an item val from the set if it presents.getRandom − This will return a random element from current set of elements. Each element must have the same probability of being returned.To solve this, we will follow these steps −For the initialization, define a parent map, and elements arrayFor the insert() function, it will take val as inputif val is not present in parent map, or ... Read More

Flatten Nested List Iterator in Python

Arnab Chakraborty
Updated on 29-Apr-2020 12:33:50

439 Views

Suppose we have a nested list of integers; we have to implement an iterator to flatten it. Each element is either an integer, or a list. The elements of that list may also be integers or other lists. So if the input is like [[1, 1], 2, [1, 1]], then the output will be [1, 1, 2, 1, 1]To solve this, we will follow these steps −In the initializing section, it will take the nested list, this will work as follows −set res as empty list, index := 0, call getVal(nestedList)The getVal() will take nestedIntegers, this will work as −for ... Read More

Course Schedule II in Python

Arnab Chakraborty
Updated on 29-Apr-2020 12:26:29

350 Views

Suppose there are a total of n courses, these are labeled from 0 to n-1. Some courses may have prerequisites, given the total number of courses and a list of prerequisite pairs, we have to find the ordering of courses that we should take to finish all courses. There may be multiple correct orders, we just need to find one of them. If it is impossible to finish all courses, then return an empty array.So if the input is like 2, [[1, 0]], then the result will be [0, 1]. There are a total of 2 courses to take. To ... Read More

Course Schedule in Python

Arnab Chakraborty
Updated on 29-Apr-2020 12:25:39

1K+ Views

Suppose there are a total of numCourses courses we have to take, labeled from 0 to numCourses-1. Some courses may have prerequisites, for example to take course 0 we have to first take course 1, which is expressed using a pair: [0, 1]. Suppose there are total number of courses that is provided and a list of prerequisite pairs, we have to check whether is it possible for you to finish all courses?So if the input is like − numCourses = 2 and prerequisites = [[1, 0]], then the result will be true, because there are a total of 2 ... Read More

Minimum Cost Tree From Leaf Values in Python

Arnab Chakraborty
Updated on 05-Mar-2020 07:33:55

436 Views

Suppose we have an array arr of positive integers, consider all binary trees such that −Each node has either 0 or 2 children;The values of arr array correspond to the values of each leaf in an inorder traversal of the tree.The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively.Among all possible binary trees considered, we have to find the smallest possible sum of the values of each non-leaf node. So if the input arr is [6, 2, 4], then the output will be 32, as there ... Read More

Maximum Nesting Depth of Two Valid Parentheses Strings in Python

Arnab Chakraborty
Updated on 02-May-2020 11:03:09

309 Views

Suppose we have a string, that string is a valid parentheses string (denoted VPS) if and only if it consists of "(" and ")" characters only, and it satisfies these properties −It is the empty string, orIt can be written as AB, where A and B are VPS's, orIt can be written as (A), where A is a VPS.We can also define the nesting depth depth(S) of any VPS S like below −depth("") = 0depth(A + B) = max of depth(A), depth(B), where A and B are VPS'sdepth("(" + A + ")") = 1 + depth(A), where A is a ... Read More

Delete Nodes And Return Forest in Python

Arnab Chakraborty
Updated on 05-Mar-2020 07:26:02

258 Views

Suppose we have the root of a binary tree, each node in the tree has a unique value. After removing all nodes with a value in to_delete, we are left with a forest. We have to find the roots of the trees in the remaining forest. So if the input is likeif the to_delete array is like [3, 5], then the output will beTo solve this, we will follow these steps −Define an array resDefine a method solve(), this will take node, to_delete array and a Boolean type info to which is telling that the node is root or not. ... Read More

Filling Bookcase Shelves in Python

Arnab Chakraborty
Updated on 05-Mar-2020 07:15:01

607 Views

Suppose we have a sequence of books − Here the i-th book has thickness books[i][0] and height books[i][1]. If we want to place these books in order onto bookshelves that have total width shelf_width. If we choose some of the books to place on this shelf (such that the sum of their thickness is = 0 and temp – books[j, 0] >= 0, docurr_height := max of books[j, 1], curr_heightdp[i] := min of dp[i], curr_height + (dp[j - 1] if j – 1 >= 0, otherwise 0)temp := temp – books[j, 0]decrease j by 1return last element of dpLet us ... Read More

Path With Maximum Minimum Value in Python

Arnab Chakraborty
Updated on 05-Mar-2020 07:11:48

472 Views

Suppose we have a matrix A of integers with R rows and C columns, we have to find the maximum score of a path starting from [0, 0] and ending at [R-1, C-1]. Here the scoring technique will be the minimum value in that path. For example, the value of the path 8 → 4 → 5 → 9 is 4. A path moves some number of times from one visited cell to any neighboring unvisited cell in one of the 4 cardinal directions (north, east, west, south).For example, if the grid is like −545126746The orange cells will be the ... Read More

Advertisements