Problems


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

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

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

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

Read

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

Read

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

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

Jump game

This problem could definitely be solved by dynamic programming, but if we take greedy approach, we can solve it in linear time. We can iterate through the array from last index to first index. We set...

Read
... 86 87 88 89 90 ...