Problems


July 18, 2022

Encode and decode string

We will join the array using $$ as delimeter, and also split the string with $$ to get it back. Time Complexity: O(n), where n is the length of the array Space...

Read

July 18, 2022

Find largest value in each tree row

This is a classic BFS problem. We will traverse the tree with BFS and we will find the maximum value in each level. After each level is done, we will push the max value in our result array. After the...

Read

July 18, 2022

Lowest common ancestor of a binary search tree

We are guranteed to find a common ansestor. That means if the 2 values of p and q have their value both on left side or the tree root, right side of the tree root or opposite side of tree root. As...

Read

July 18, 2022

Minimum path 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, if our current position is out of bound, then...

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 17, 2022

Coin change

This is a classing dynamic programming problem. First we will solve it in brute force and then memoization(top-down) to reduce complexity. Time Complexity: O(n*a), n is the number of...

Read

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

Best time to buy and sell stock

This is a classic sliding window problem. We will take 2 pointers, left pointer at the first element and right pointer as the second element. Then we forward right pointer and compare if the value at...

Read

July 17, 2022

House robber

We will first solve this with brute force and then use memoization to optimize our solution. For this problem, if we have no item, then the sum would be zero, so it would be our base case, we have 2...

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