Problems
July 31, 2022
reverse-integer
We can get the least significant digit by moding it with 10. Then we can also get the remainder of the number by divide it by 10. The only catch of this problem is if the reversed number overflows 32...
ReadJuly 31, 2022
Sum of two integers
We know, 2^a + 2^b = 2^(a+b)
and log2(2^a) = a
. We can use this two formula to sum 2 numbers without using +
.
Time Complexity: O(1)
Space...
July 30, 2022
Car fleet
First we will create a pair with position and speed, and sort it by the position. Then we start at the position which is closer to the target. Then we will calculate how much time it's needed to...
ReadJuly 30, 2022
Daily temperatures
We will initialize the result array and assign zero to each elements. We will iterate through the temperatures, and if the stack is empty or stack top value is less that the current temperature, then...
ReadJuly 30, 2022
Largest rectangle in histogram
First we go through all the heights in a loop. We also keep a stack to calculate max area. If stack is empty, then we push the height along with the index it sits on. Then we check whether the height...
ReadJuly 30, 2022
Top k frequent words
First we will count all the words and then create a max heap with the count, each element will have 2 items, first will be the count, second will be the word itself. Then we pop k elements and put...
ReadJuly 30, 2022
Word search II
The problem is an extension of the problem word search. We will run backtracking DFS to find the word. But as there are multiple word to search from, we will use a trie data structure, so we can...
ReadJuly 30, 2022
Word subsets
First we count all the characters in our search array and combine it to a hashmap containing all the characters count. Then we iterate through each word in our input word list, count all the...
ReadJuly 29, 2022
Find and replace pattern
We will create a hashmap for each word counting the length of the hashmap at that position. Then we create the same hashmap for our pattern. Then we iterate over each word, match it with the pattern...
ReadJuly 29, 2022
Generate parentheses
We will solve it with backtracking. We will add one open parentheses, then call the recusive backtracking method. If the number of opening parentheses and closing parentheses are equal to input, that...
Read