Problems
July 16, 2022
Course schedule II
This is a classing cycle detect graph problem. We will first convert our edge list to adjacency list. Then we use white-grey-black algorithm to detect cycle. At first every node will be in our white...
ReadJuly 16, 2022
Valid parentheses
We can iterate over each characters of the string, and if it's a opening braket, we push it to the stack. If not, then we check, if it matches the top of the stack braket. If matches then we pop it...
ReadJuly 16, 2022
Course schedule
This is a classing cycle detect graph problem. We will first convert our edge list to adjacency list. Then we use white-grey-black algorithm to detect cycle. At first every node will be in our white...
ReadJuly 16, 2022
Reverse bits
We can iterate over all 32 bits of our given number, then put it on the opposite bit or our result. If we take i-th bit, we will put it in the (31-i)th bit of our result. Time Complexity: O(1), as...
ReadJuly 16, 2022
Number of 1 bits
We can have a repeatative pattern if we look at the most significant bit. For example, lets look the following table- Number Binary 0 000 1 001 2 010 3 011 4...
ReadJuly 15, 2022
Merge two sorted list
This approach is very much like merge sort. We took 2 pointers for 2 list, compare thier values, and assign the smaller one to our new list. Then we move that pointer to next node. We go through the...
ReadJuly 15, 2022
Reverse linked list
We start with a null node called previous, iterate through the list, and move it's next printer previous on and then repeat it till the end. Then we just return the previous which is not the current...
ReadJuly 15, 2022
Container with most water
We will take two pointers, one at the beginning and another at the end. Then we calculate the water in between by taking the min value of this 2 position and multiply by the difference of these...
ReadJuly 14, 2022
Balanced binary tree
We will traverse the tree with DFS and also keep track of the depth. Then return both the balanced and depth from our DFS recursion. Then we just compare whether the difference between the left and...
ReadJuly 14, 2022
Subtree of another tree
We can check whether two tree are same using the same logic from Same tree problem. Then we will check recursively for every subtree whether that is same as out target subtree. Time Complexity:...
Read