A binary tree may contain subtrees that are valid binary search trees (BST). We need to find the largest subtree (with maximum number of nodes) that forms a valid BST from the given binary tree. So, if the input is like ? 12 3 5 4 6 ... Read More
Suppose we have an N x M binary matrix where 0 represents an empty cell and 1 represents a blocked cell. Starting from the top-left corner, we need to find the number of ways to reach the bottom-right corner. We can only move right or down. So, if the input matrix is ? 001 000 110 Then the output will be 2, as there are two valid paths: [Right, Down, Right, Down] and [Down, Right, Right, Down]. Algorithm We use dynamic programming to solve this problem. The approach involves ? ... Read More
Sometimes we need to count sublists that contain exactly k unique elements from a given list. This problem can be solved using a sliding window approach with the key insight that sublists with exactly k unique elements equals sublists with "at most k" minus sublists with "at most k-1" unique elements. So, if the input is like nums = [2, 2, 3, 4] and k = 2, then the output will be 3, as we have the sublists like: [2, 2, 3], [2, 3], [3, 4]. Algorithm Approach To solve this, we will follow these steps ? ... Read More
Given a list of numbers and a value k, we need to find the lexicographically smallest subsequence of size k. A subsequence maintains the relative order of elements from the original list. Problem Understanding For example, if we have nums = [2, 3, 1, 10, 3, 4] and k = 3, we need to select 3 numbers that form the smallest possible sequence when compared lexicographically. The answer would be [1, 3, 4]. Algorithm Approach To solve this problem, we use a greedy approach ? For each position in our result, find the smallest ... Read More
Suppose we have a list of intervals where each interval contains three values [start, end, profit]. We can perform only one task at a time, and we have to find the maximum amount of profit we can get by scheduling non-overlapping jobs. So, if the input is like intervals = [[1, 2, 100], [3, 5, 40], [6, 19, 150], [2, 100, 250]], then the output will be 350, as we can take these two intervals [1, 2, 100] and [2, 100, 250]. Algorithm Overview To solve this problem, we will use dynamic programming with the following approach: ... Read More
Given a list of numbers and a value k, we need to find the number of strictly increasing subsequences of size k. A subsequence maintains the relative order of elements but doesn't need to be contiguous. So, if the input is like nums = [2, 3, 4, 1] and k = 2, then the output will be 3, as we have the subsequences of size 2: [2, 3], [3, 4], [2, 4]. Approach Using Dynamic Programming We use dynamic programming where dp[i] represents the number of increasing subsequences of current length ending at position i ? ... Read More
Suppose we have two lists of numbers nums0 and nums1 of the same length and two other values d as distance and c as cost. If we start from index 0 at either nums0 or nums1 and want to end up at the final index of either list. Now, in each round, we can select to switch to the other list for cost of cost. Then we can jump forward at most d distance away where the cost of landing at an index is the value at that point. So we have to find the minimum total cost possible to ... Read More
Suppose we have a list of sorted numbers called stones representing the positions of stones on a river that we are trying to cross. To cross the river, we must finish at the last stone. In each step, we can jump (k - 1, k, or k + 1) steps ahead where k is the distance of the last jump. We have to check whether we can cross the river or not. So, if the input is like stones = [0, 1, 3, 4, 5, 6, 8, 9, 13], then the output will be True. We can start from ... Read More
Suppose we have a list of colors (R, G, B). When two different colors are adjacent, they can merge into a single color item of the third color. We need to find the minimum number of colors remaining after any possible sequence of such transformations. So, if the input is like colors = ["G", "R", "G", "B", "R"], then the output will be 1 as it can transform like below ? Initial: G R G ... Read More
When given a binary matrix where 0 represents water and 1 represents land, we need to find the land cell with the longest Manhattan distance from any water cell. The Manhattan distance is the sum of absolute differences of coordinates. So, if the input matrix is ? 1 1 1 1 1 1 0 1 1 1 1 1 0 0 1 1 Then the output will be 3, as the cell at [0, 0] has a Manhattan distance of 3 from the nearest water. Algorithm ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance