Problems
July 24, 2022
Permutations II
First we will count the number of each element, then we create our decision tree, if we take one element, then we remove that element form our count hashmap. If it has multiple instance, we reduce...
ReadJuly 24, 2022
Permutations
We will solve it recursively using backtracking. If there is only one element in the list, permutations of this will return the same list. This will be our base case. If it has 2 elements, we remove...
ReadJuly 24, 2022
Search a 2d matrix II
if the current grid value matrix[r][c] is less than the target, that means, we don't need to search in this row anymore, as all the values are already bigger than the matrix[r][c]. If the value is...
ReadJuly 24, 2022
Search a 2d matrix
We can just consider the matrix as a linear sorted array. Then if we need an element number i in our flattened list, we can get it by matrix[i//col][i%col]. Then we can do a regular binary search on...
ReadJuly 24, 2022
Subsets II
We will first sort the element. Then like the original subset problem, we will have 2 choice for each element, either choose it or skip it. As we don't want duplicate subset, we will skip the same...
ReadJuly 24, 2022
Word search
We will run dfs on each element of the board, if we find a character match, then we move forward and search for next character in all 4 directions.
Time Complexity: O(n*m*4^n)
Space...
July 23, 2022
3Sum
If you already solved the 2 sum ii problem, you might get the idea. First we will sort the list. Then we take the first element at first position, then for the rest of the elements we take 2...
ReadJuly 23, 2022
Design add and search words data structure
We will implement the trie as normal, the addWord function will be identical to a normal trie. The search function will be a little different. We will iterate over each character, if the character is...
ReadJuly 23, 2022
K closest points to origin
First we will calculate all the distances from the origin and put that on a list. Then we heapify that list, then pop top k elements and put their coordinate on a result array and return that....
ReadJuly 23, 2022
Kth largest element in an array
We can just sort the element and return second largest element from the array, but it will have O(nlog(n))
time complexity. But if we use a heap, then the time complexity will be...