Problems


July 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...

Read

July 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...

Read

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...

Read

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....

Read

July 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...

Read

July 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,...

Read

July 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...

Read

July 22, 2022

N-queens

We will have 3 different sets, one for the column, one for positive diagonal and one for negative diagonal. We will iterate over the each row, and try to add the queen in the board. If this is...

Read

July 22, 2022

Partition list

We will have 2 separate linked list, left and right. All the values that is less than target will go to the left list, all the values that is greater than or equal to target will go to the right...

Read

July 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
... 86 87 88 89 90 ...