Problems
July 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...
July 23, 2022
Last stone weight
We will use a max heap and then pop 2 element at a time and simulate the problem statement. As python doesn't have any max heap, we will use the min heap but multiply each element with -1 and the the...
ReadJuly 23, 2022
Meeting rooms II
We will sort the intervals, then in each iteration we will compare the end of the meeting with the start of the previous meeting. We will keep track of the count for the number of meeting going on,...
ReadJuly 23, 2022
Plus one
First we reverse the list, then add one to the fist item, if the number is 9, then we keep a carry and add it to the next number. We will do that until all the digit in the list have been iterated...
ReadJuly 23, 2022
Remove nth node from end of list
We will have 2 pointer, first we move the right pointer to nth node. Then we move both left and right pointer until the right pointer moves to the end of the list, that means left pointer is now on...
ReadJuly 23, 2022
Subsets
We will do backtracking to solve the result. For each element of the list, we have 2 option, either we take the element or we leave the element. From this base case, if we run dfs for from the...
ReadJuly 23, 2022
Task scheduler
We will first count each characters and create a max heap out of it. Then we pop the item from the heap, put it on a queue, and increase the value of our counter. Then we process the task, if there...
ReadJuly 23, 2022
Time based key value store
We will keep a hashmap as the key value store, key will be used as the key, value will have an array storing all the values and timestamp. When we search for the value, we will look up in the store...
ReadJuly 22, 2022
Jump game II
This is very similar to jump game, we will go for a greedy approah. We will have 2 pointer, left and right to determine the level of each move. In every iteration we will calculate the maximum number...
Read