Problems
September 5, 2022
House robber III
We will traverse the tree in DFS and take both with and without root result. Then we calculate the max from both of them and return the result.
Time Complexity: O(n)
Space...
September 5, 2022
Basic calculator
We will take a stack. Initially our operation and operand will be 0 and +
append each character to the stack. If the character is numeric, until we reach an operand we will calculate the...
September 5, 2022
Longest absolute file path
Use stack to represent structure of a directory where the root of a directory is at index 0. Directory level can be infer from the number of tabs. ie "" is level 0, "\n" is level 1 and so on. Start...
ReadSeptember 5, 2022
Course schedule IV
We will first create an adjacency list from the prerequisites. Then for each queires, we will start DFS from course 1 to course 2, if we can reach to course 2, then we append true in our result, else...
ReadSeptember 5, 2022
All ancestors of a node in a directed acyclic graph
First we will create an ancestors adgacency list, where we can get the immediate parent of each node from the edge list. Then we will run DFS from each node, and get the node list of all reachable...
ReadSeptember 5, 2022
Remove duplicate letters
We will create a hashmap to keep track of last occurance of the letters. Then we will have a visited hashset and a stack to keep track of the letters. We traverse sequentially on the string, for each...
ReadSeptember 5, 2022
Search in rotated sorted array II
To solve this problem we have to follow the folllowing steps: Calculate the mid index. Check if the mid element == target, return True else move to next step. Else if the mid element >= left. If...
ReadSeptember 5, 2022
Single number III
We will be using the XOR trick i.e n^n=0
. During first traversal we will get the XOR of the required two numbers, then we will get the least significant bit of the XOR operation. This...
September 5, 2022
Single number II
We will have 2 elements, one will be store all the values that store all the elements that occurs once and two will store all the elements that occurs twice. Then we make sure that the values that is...
ReadSeptember 5, 2022
Basic calculator III
We will take a stack. Initially our operation and operand will be 0 and +
append each character to the stack. If the character is numeric, until we reach an operand we will calculate the...