Problems


September 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...

Read

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...

Read

September 5, 2022

Linked list cycle II

We can use Floyd's cycle detection algorithm to find the position. We will create a fast and slow pointer, fast pointer is twice as fast as the slow pointer. If the two pointer doesn't meet, that...

Read

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...

Read

September 5, 2022

Number of enclaves

We will run DFS from every bordering position of the grid and turn the 1's to 0's. Then we will just count the number of position where we still have 1's left, and return that count as result....

Read

September 5, 2022

Path sum II

We will use DFS along with backtracking to get the paths of every root to leaf path that adds upto the target. Time Complexity: O(n) Space Complexity: O(n), for...

Read

September 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...

Read

September 5, 2022

Restore ip addresses

We will create a decision tree based on the rule that each segment of the ip address can be between 0 and 255. Then we run our DFS to get all possible options and return the result. Time...

Read

September 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...

Read

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...

Read
... 63 64 65 66 67 ...