Found 10784 Articles for Python

Course Schedule II in Python

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

248 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

788 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

354 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

205 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

178 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

398 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

370 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

Smallest Subsequence of Distinct Characters in Python

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

135 Views

Suppose we have a text, we have to find the lexicographically smallest subsequence of text that contains all the distinct characters of text exactly once. So if the input is like “cdadabcc”, then the output will be “adbc”.To solve this, we will follow these steps −Define a stack st, two maps last_o and considered, they are initially blankfor i in range length of text – 1 down to 0if text[i] is not present in last_o −last_o[text[i]] := iconsidered[text[i]] := falsei := 0while i < length of textif stack has no elementspush text[i] into stackconsidered[text[i]] := trueincrease i by 1otherwise stack ... Read More

Insufficient Nodes in Root to Leaf Paths in Python

Arnab Chakraborty
Updated on 05-Mar-2020 06:54:00

197 Views

Suppose we have a binary tree. A node is known as insufficient if every such root to leaf path intersecting this node has sum strictly less than limit. We have to delete all insufficient nodes simultaneously, and return the root of the resulting binary tree. So if the tree is like, and the limit is 1 −Then the output tree will be −To solve this, we will follow these steps −Define a method solve(), this will take root and limitif node has no left and right subtree, thenif value of root is less than 1, return null, otherwise rootif root ... Read More

Grumpy Bookstore Owner in Python

Arnab Chakraborty
Updated on 02-May-2020 11:00:29

150 Views

Suppose a bookstore owner has a store open for number of customers list entries minutes. In every minute, some number of customers (customers[i]) enter the store, after that all those customers leave after the end of that minute. On some minutes, the owner is grumpy. Now if the owner is grumpy on the i-th minute, then grumpy[i] = 1, otherwise grumpy[i] = 0. When the bookstore owner is grumpy, the customers of that minute are unhappy, otherwise they are happy. The bookstore owner knows a technique to keep themselves not grumpy for X minutes straight. That technique cannot be used ... Read More

Advertisements