Problems


August 16, 2022

N-th tribonacci number

This problem is just like fibonacci number, we will first solve it recursively and then memoize it to reduce redundant calculation. Time Complexity: O(n) Space Complexity:...

Read

August 16, 2022

String to integer atoi

First we will strip any whitespace characters, and then check if the length is zero, we immediately return 0. Then we check the sign, if the sign is + or -, we keep that in...

Read

August 15, 2022

Prime number of set bits in binary representation

We will create 2 helper function, one for checking the prime number, another for counting the number of 1 bits. Then we iterate over the range, then count the number of bits, and then check whether...

Read

August 15, 2022

Roman to integer

We can replace characters like IV or IX to IIII and VIIII. In that way, when we calculate our result, we can just translate the value to integer...

Read

August 14, 2022

Next greater element I

We traverse nums2 and start storing elements on the top of stack. If current number is greater than the top of the stack, then we found a pair. Keep popping from stack till the top of stack is...

Read

August 14, 2022

Word ladder II

We will first run a BFS to create the graph with adjacency list. Then we will run DFS to find all possible paths of that graph. Time Complexity: O((n+m)*l), n is the number of words,...

Read

August 13, 2022

Design circular queue

We can implement a circular queue by using an array, the maximum size of the array will be the given input k. Then we will keep 2 pointers, one for head and one for tail, and whenever we enqueue some...

Read

August 13, 2022

Distinct subsequences

We will check each character of both string, when we find a match, then we have 2 choice, either we include the current index or we skip it. If we don't have a match, we we will skip the current...

Read

August 13, 2022

Edit distance

To compute the minimum distance, we will recursively call our decision tree. If i is 0, then we need to insert j characters, if j = 0, then we need in delete i characters. This will be our base case,...

Read

August 13, 2022

Regular expression matching

We will go through our decision tree, and then we have 2 choices, either we take the * or we skip it. Based on this logic, we will create a recursive DFS solution for our problem. Then we memoize it...

Read
... 74 75 76 77 78 ...