Problems
November 23, 2022
Final prices with a special discount in a shop
We will iterate over the prices array. For each price, we will iterate over the prices array again to find the first price that is less than or equal to the current price. If we find the price, we...
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
Power of two
We can solve the problem iteratively. If the number is even, we can divide it by 2. If the number is odd, we can return false. We can use bit manipulation to check if the number is odd. Time...
ReadNovember 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 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 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
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,...
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...
Read