Problems
December 17, 2022
Replace elements in an array
We will use a hashmap to store the index of the input array for constant time lookup. Then we will iterate through the operations, and for each operation, we will update the value of the input array....
ReadDecember 17, 2022
Root equals sum of children
We will check the value of root with the sum of the rest of the tree. If the value of root is equal to the sum of the rest of the tree, then we will return True
. Otherwise, we will...
December 17, 2022
Ternary expression parser
We will use a stack to evaluate the expression. We start from the end of the expression, then append the value to the stack. If the number of element in the stack is more than 2, and the second last...
ReadDecember 16, 2022
Encode number
The following sequence can be built up form the ealier result. So we can search index of the prefix part, for example: f(5) = "10", f(6) = "11" The prefix are both f(2) = "1" so we found that f(n)...
ReadDecember 16, 2022
Number of closed islands
We will use a DFS approach to solve this problem. We will iterate through the grid. We will check if the current cell is a land. We will check if the current cell is a closed island. We will return...
ReadDecember 16, 2022
Widest pair of indices with equal range sum
We will use a prefix sum approach to solve this problem. We will create a prefix sum array. We will create a hashmap. We will iterate through the prefix sum array. We will calculate the difference...
ReadDecember 15, 2022
Campus bikes II
We will use a dynamic programming approach to solve this problem. We will create a memoization table. We will iterate through the memoization table. We will iterate through the bikes. We will check...
ReadDecember 14, 2022
Append k integers with minimal sum
We will use a greedy approach to solve this problem. We calculate the sum of the first k
integers. We will iterate over the array and if the current number is less than k
,...
December 14, 2022
Count number of homogenous substrings
We will use a sliding window approach to solve this problem. We will iterate over the string and keep track of the start and end of the current substring. We will increment the end of the substring...
ReadDecember 14, 2022
Minimum number of operations to move all balls to each box
The question needs to search for balls left and right relative to the current box. If we use [1,1,0] as example, in the second box (i.e. boxes[1]): You will need to find the first box value (go...
Read