Problems
August 2, 2022
Kth smallest element in a sorted matrix
We will create a max heap and insert each element there. If the length of the heap is more than k, we pop item from the heap. After the iteration is done, we return to top of the heap. As python does...
ReadAugust 2, 2022
Permutation in string
We will first count the characters in string1 and then we will slide a window of length of string1 and count the matches of the characters of string1 and sliding window. If the number of matches is...
ReadAugust 1, 2022
Add two numbers
We will add first node from both list, and add it to a new list. We will also keep a carry if the number if greater than 9. We will run it until all of l1, l2 and carry is null and return the head of...
ReadAugust 1, 2022
Copy list with random pointer
We will traverse the whole list twice, first time we create a hashmap to store all the nodes without any linking. Then we traverse it again, but this time, as we already have all the nodes, we will...
ReadAugust 1, 2022
Koko eating bananas
We will search through each position of the piles till the largest pile, where it allows us to eat all the bananas within given h hours. We can do it in brute force, but if we search with binary...
ReadAugust 1, 2022
Reorder list
We will first find the middle of the list with a fast and slow pointer, fast pointer is twice as fast as the slow pointer. Then we will reverse the second half of the linked list. Finally, we merge...
ReadJuly 31, 2022
Climbing stairs
We will first solve it in recursive brute force, the use memoization to make it efficient. If the number of stairs is less than or equal to 1, then we can climb the stair with 1 step, this will be...
ReadJuly 31, 2022
Largest number at least twice of others
We will keep track of both largest and second largest number while iterating through the whole nums array. Then if the largest number is at least twice as big as the second largest, then we return...
ReadJuly 31, 2022
Min cost climbing stairs
We will follow the similar approach as Climbing stairs, first solve it with recursion, the use memoization to make it efficient. If the number of stairs is less than 0, then the cost of climbing it...
ReadJuly 31, 2022
Perfect squares
This problem is exactly like the coin change problem. But we are not given a list of squares, we need to generate it. The biggest squares root will be the square root of the given number, without any...
Read