Found 26504 Articles for Server Side Programming

Minimum Cost Tree From Leaf Values in Python

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

437 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

312 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

261 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

615 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

475 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

Shortest Path in Binary Matrix in C++

Arnab Chakraborty
Updated on 02-May-2020 11:01:56

833 Views

Suppose we have an N by N square grid, there each cell is either empty (0) or blocked (1). A clear path from top-left to bottom-right has length k if and only if it is composed of cells C_1, C_2, ..., C_k such that −Adjacent cells C_i and C_{i+1} are connected 8-directionally (So they are different and share an edge or corner)C_1 is at location (0, 0)C_k is at location (N-1, N-1)If C_i is located at (r, c), then grid[r, c] is empty or contains 0We have to find the length of the shortest such clear path from top-left to ... Read More

Smallest Subsequence of Distinct Characters in Python

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

201 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

280 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

239 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

Binary Search Tree to Greater Sum Tree in C++

Ravi Ranjan
Updated on 04-Sep-2025 15:02:56

445 Views

In this article, we are given a binary search tree. Our task is to transform the given BST to a Greater Sum tree. A Greater Sum Tree with respect to the given BST is a tree where each node's value is replaced by the sum of all values greater than that node's value in the original BST. Below is an example scenarios to convert the given BST to a Greater Sum Tree: Example Scenario Input: BST inorder traversal= {0, 1, 2, 3, 4, 5, 6, 7, 8} Output: Greater Sum Tree = {36, 35, 33, 30, 26, 21, 15, ... Read More

Advertisements