When working with binary trees, we sometimes need to delete specific nodes and return the remaining forest of trees. This problem requires us to remove nodes with values in a given list and return the roots of all remaining subtrees. Problem Understanding Given a binary tree and a list of values to delete, we need to ? Remove all nodes with values in the to_delete list Return the roots of all remaining subtrees (the forest) When a node is deleted, its children become new roots if they exist ... Read More
The bookshelf filling problem involves arranging books with given thickness and height onto shelves of limited width while maintaining their original order. We need to find the minimum possible total height of the bookcase. Each book has dimensions books[i][0] (thickness) and books[i][1] (height). Books must be placed in the given order, and each shelf has a maximum width of shelf_width. 1, 1 2, 3 2, 3 ... Read More
In this problem, we need to find the maximum possible minimum value along any path from the top-left corner [0, 0] to the bottom-right corner [R-1, C-1] of a matrix. A path can move in four cardinal directions (north, east, west, south), and the score is determined by the smallest value encountered along that path. For example, if we have a path 8 → 4 → 5 → 9, the path's score would be 4 (the minimum value). Problem Example Consider this matrix ? 5 4 5 1 2 6 ... Read More
Finding the lexicographically smallest subsequence of distinct characters is a classic problem that can be solved using a greedy approach with a stack. Given a string, we need to find the smallest subsequence that contains all unique characters exactly once. For example, if the input is "cdadabcc", the output should be "adbc". Algorithm Overview The approach uses a stack to build the result and two dictionaries to track character positions and inclusion status ? last_occurrence: Stores the last position of each character in_stack: Tracks whether a character is already in the result stack stack: Builds ... Read More
When working with binary trees, we sometimes need to remove nodes based on path sum criteria. A node is insufficient if every root-to-leaf path passing through that node has a sum strictly less than a given limit. This problem requires us to delete all insufficient nodes simultaneously and return the modified tree. Problem Understanding Given a binary tree and a limit value, we need to identify and remove nodes where all paths from root to leaf through that node have sums less than the limit. The key insight is that we work backwards from leaves, checking if any ... Read More
The Grumpy Bookstore Owner problem is a classic sliding window optimization challenge. A bookstore owner can use a technique to stay calm for X consecutive minutes, and we need to find the maximum number of happy customers by choosing the optimal window. Problem Understanding Given three inputs ? customers[i] − number of customers in minute i grumpy[i] − 1 if owner is grumpy, 0 if calm X − technique duration (consecutive minutes) When grumpy, customers are unhappy. We can use the technique once to make the owner calm for X minutes. Find the maximum ... Read More
The Capacity to Ship Packages Within D Days problem asks us to find the minimum ship capacity needed to transport all packages within a given number of days. Given an array of package weights and D days, we need to determine the least weight capacity that allows shipping all packages sequentially. Problem Understanding For example, with weights [3, 2, 2, 4, 1, 4] and D = 3 days, the minimum capacity is 6 ? Day 1: 3, 2 (total weight: 5) Day 2: 2, 4 (total weight: 6) Day 3: 1, 4 (total weight: 5) ... Read More
A Binary Search Tree (BST) can be constructed from its preorder traversal using a stack-based approach. In preorder traversal, we visit the root first, then the left subtree, followed by the right subtree. 8 5 10 1 7 12 ... Read More
Suppose we have the root of a binary tree, where each node contains a value from 0 to 25, representing the letters 'a' to 'z': a value of 0 represents 'a', a value of 1 represents 'b', and so on. We have to find the lexicographically smallest string that starts at a leaf of this tree and ends at the root. z(25) b(1) d(3) b(1) ... Read More
A basic calculator evaluates mathematical expressions containing non-negative integers and operators (+, -, *, /). The calculator must handle operator precedence correctly, where multiplication and division are performed before addition and subtraction. For the input "3+2*2", the output should be 7 because multiplication has higher precedence than addition. Algorithm Approach We use a stack-based approach to handle operator precedence ? Remove spaces from the expression Use a stack to store intermediate results Process multiplication and division immediately Store addition and subtraction operands for later summation Implementation class Solution: ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance