Server Side Programming Articles - Page 1867 of 2650

Filling Bookcase Shelves in Python

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

630 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

490 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

853 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

210 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

296 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

248 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

550 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

Maximum Difference Between Node and Ancestor in C++

Arnab Chakraborty
Updated on 02-May-2020 10:51:08

201 Views

Suppose we have the root of a binary tree, we have to find the maximum value V for which there exists different nodes A and B where V = |value of A – value of B| and A is an ancestor of B. So if the tree is like −Then the output will be 7. The ancestor node differences are like [(8 - 3), (7 - 3), (8 - 1), (10-13)], among them the (8 - 1) = 7 is the maximum.To solve this, we will follow these steps −initially define ans 0define one method called solve(), this will take ... Read More

Capacity To Ship Packages Within D Days in Python

Arnab Chakraborty
Updated on 02-May-2020 10:46:47

356 Views

Suppose there is a conveyor belt has packages that will be shipped from one port to another within D days. Here the i-th package on the conveyor belt has a weight of weights[i]. Each day, we will load the ship with packages on the belt. We will not load more weight than the maximum weight capacity of the ship. We have to find the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within D days. So if the input is like [3, 2, 2, 4, 1, 4] and D ... Read More

Construct Binary Search Tree from Preorder Traversal in Python

Arnab Chakraborty
Updated on 02-May-2020 10:45:57

821 Views

Suppose we have to create a binary search tree that matches the given preorder traversal. So if the pre-order traversal is like [8, 5, 1, 7, 10, 12], then the output will be [8, 5, 10, 1, 7, null, 12], so the tree will be −To solve this, we will follow these steps −root := 0th node of the preorder traversal liststack := a stack, and push root into the stackfor each element i from the second element of the preorder listi := a node with value iif value of i < top of the stack top element, thenleft of ... Read More

Advertisements