Problems


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

Read

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

Read

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

Read

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

Read

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

Read

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

Read

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

Read

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

Read

July 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

July 14, 2022

Two sum II - input array is sorted

As the array is sorted, we will take 2 pointer at the beginning and the end. If the sum of two numbers are equal to target, we return. If the sum is bigger than target, that means we need a smaller...

Read
... 90 91 92 93 94 ...