Problems
July 31, 2022
Climbing stairs
We will first solve it in recursive brute force, the use memoization to make it efficient. If the number of stairs is less than or equal to 1, then we can climb the stair with 1 step, this will be...
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
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
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
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
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
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
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
Longest consecutive sequence
We will first create a set with the input numbers, so we can look up in the set with constant time. Then we iterate over the numbers, check if the current number has any predecisior, if not that...
ReadJuly 29, 2022
Search suggestions system
First we will sort the products list. We will go through each character, create a search substring and then check if the product from the products list starts with the search substring. If yes, we...
Read