Problems
November 23, 2022
Broken calculator
We can simulate the result. If target is greater than X, we can only multiply X by 2, otherwise we can only subtract 1 from X. So we can use greedy to find the optimal solution. Time complexity:...
ReadNovember 23, 2022
Delete duplicate folders in system
We will use TrieNode to create the graph of folder structure. Then dfs on our graph and serialize subtrees as strings, then add nodes which has the same serialize string into a dictionary, let name...
ReadNovember 23, 2022
Sliding window median
We will use a sorted list to store the numbers in the sliding window. Then, we will iterate through the list. For each number, we will find the index of the number in the sorted list. Then, we will...
ReadNovember 23, 2022
Integer to english words
We can use a hashmap to store the mapping from number to english words. Then we can use greedy to find the optimal solution.
Time complexity: O(log(n))
, n is the value of num
Space...
November 23, 2022
Rabbits in forest
We will use a map to store the number of rabbits with the same color. Then, we will iterate through the map. For each color, we will calculate the number of groups. Then, we will add the number of...
ReadNovember 22, 2022
odd-even-jump
We need to jump higher and lower alternately to the end. Take [5,1,3,4,2] as example. If we start at 2, we can jump either higher first or lower first to the end, because we are already at the end....
ReadNovember 22, 2022
132 pattern
We will use a stack to store the numbers. Then, we will iterate through the numbers and check if the number is less than the second number. If it is, then we will pop the numbers from the stack until...
ReadNovember 22, 2022
Boats to save people
We will sort the array. Then, we will use two pointers to keep track of the start and end of the array. Then, we will iterate through the array. If the sum of the numbers at the start and end...
ReadNovember 22, 2022
Process tasks using servers
We will use two heaps to simulate. The first heap will be a min heap of the servers. The second heap will be a min heap of the tasks. Then, we will iterate through the tasks. If the task is less than...
ReadNovember 22, 2022
Number of ways to paint N×3 grid
Two pattern for each row, 121 and 123. 121, the first color same as the third in a row. 123, one row has three different colors. We consider the state of the first row, pattern 121: 121, 131, 212,...
Read