Problems
July 31, 2022
Largest number at least twice of others
We will keep track of both largest and second largest number while iterating through the whole nums array. Then if the largest number is at least twice as big as the second largest, then we return...
ReadJuly 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 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 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
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
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
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 29, 2022
Longest repeating character replacement
We will take a sliding window to solve this problem. We will take a left and right pointer, initially both will be at 0-th index. Then we move our right pointer and count the number of occurances,...
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...
Read