Problems
November 13, 2022
Number of subarrays with bounded maximum
We will use two pointers for get the range. l
is the left side of our sliding window, and r is the right side of our sliding window. We increment the size of our window (we increment...
November 13, 2022
Maximum frequency stack
We will use a hashmap freq will count the frequence of elements and another hashmap stack is a map of stack. If element x has n frequence, we will push x n times in stack[1], stack[2] .. stack[n],...
ReadNovember 12, 2022
Least number of unique integers after k removals
We can use a heap to keep track of the frequency of each number. Then we can pop the smallest frequency from the heap until we have removed k
numbers. The remaining numbers are the...
November 12, 2022
Number of zero filled subarrays
We will iterate over each number, if the number is zero, we will add the number of subarrays that can be formed with the previous numbers to the result. We will also add the number of subarrays that...
ReadNovember 12, 2022
Leaf similar trees
We will run a depth-first search on both trees and compare the leaves.
We can also achieve the same result by using a python generator instead.
Time Complexity: O(n+m)
Space...
November 11, 2022
Removing minimum number of magic beans
We will sort the beans. Then for each element, we will take the totalSum-((arrLen-currIdx)*currNum)
and take the minimum of all such values.
Time Complexity: O(nlog(n))
...
November 11, 2022
Count numbers with unique digits
For the first (most left) digit, we have 9 options (no zero); for the second digit we used one but we can use 0 now, so 9 options; and we have 1 less option for each following digits. Number can not...
ReadNovember 10, 2022
Remove all adjacent duplicates in string
We will use a stack to keep track of the characters in the string. We will then iterate through the string and if the stack is empty or the top of the stack is not equal to the current character, we...
ReadNovember 10, 2022
Maximum nesting depth of the parentheses
We will use a stack to keep track of the maximum depth of the parentheses. We will then iterate through the string and if the current character is an opening parenthesis, we will push the current...
ReadNovember 10, 2022
Remove all adjacent duplicates in string II
We will use a stack to keep track of the characters in the string. We will then iterate through the string and if the stack is empty or the top of the stack is not equal to the current character, we...
Read