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
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
Missing number
We can XOR the same number to even it out, for example 3^3 or 5^5 will always be 0. That means we can XOR every number form 1 to number of element in our input array and then XOR the input numbers...
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
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 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 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 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
Construct binary tree from preorder and inorder traversal
The first element of the preorder traversal is always the root. So, if we know the root, we can then find the left and right sub tree from inorder traversal. We will then create the tree recursively...
Read