Problems
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
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
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
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
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 22, 2022
Shuffle an array
We will use a list to store the original array. Then, we will use a list to store the shuffled array. Then, we will use a random number generator to generate a random number. Then, we will swap the...
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
Random pick index
We will use a dictionary to store the indices of the numbers. Then, we will iterate through the given array and store the indices of the numbers in the dictionary. Then, we will use a random number...
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
Split array largest sum
We binary search largest sum in range [max(nums), sum(nums)], which is left = max(nums), right = sum(nums). mid = left + (right - left) // 2 If canPartition(mid) then: Update the answer so far...
Read