Problems


July 17, 2022

Target sum

We will first solve it with brute force using recursion and then use memoization to make it efficient. For recursion, we can think about the base case, we will start our index at the number of...

Read

July 17, 2022

Unique paths

We will first solve it with brute force using recursion and then use memoization to make it efficient. For recursion, we can think about the base case, if our current position is out of bound, then...

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

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

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

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

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

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

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
... 90 91 92 93 94 ...