Problems
September 8, 2022
Combination sum III
We will run DFS towards our decision tree, for building a decision tree, we have 2 options, either we take the number or we skip the number. We will check when the sum of the numbers we took is equal...
ReadSeptember 8, 2022
Find k closest elements
We will iterate over our input array, find the absolute diffrece of each elements form target x, and put them into a heap alogn with the actual number. Then we pop k numbers from the heap, sort the...
ReadSeptember 8, 2022
Maximum erasure value
We will use a set to keep track of repeating numbers. We will have 2 pointers, we will move our right pointer, check if the number as right pointer is already exist in the set, if exists, then we...
ReadSeptember 8, 2022
Minimum consecutive cards to pick up
We will use a set to keep track of cards in the hand. Initially we will assume the current number of cards in the hand in infinity. We will have 2 pointers, we will move our right pointer, check if...
ReadSeptember 8, 2022
Repeated dna sequences
We will use a sliding window which is 10 characters long. For each window, we will add this to a lookup set. If the window is already present in the lookup set, that means we already have it more...
ReadSeptember 8, 2022
Robot bounded in circle
The idea is, after the first iteration, if the direction is not north facing or the current position is the start position, then it is a cycle, otherwise not. First if condition basically encounters...
ReadSeptember 7, 2022
Construct string from binary tree
We will use recursive DFS to traverse the tree and append the result to a string and return it.
Time Complexity: O(n)
Space Complexity: O(n)
September 7, 2022
Keys and rooms
We will create a set with all the nodes. First we put them in unvisited set. Then we start traversing the graph using BFS and remove nodes from unvisited set after every node visit. Finally when the...
ReadSeptember 7, 2022
Print binary tree
First we will calculate the height of the tree. Our result will be a 2d matrix where the each row will have 2^height-1 element and there will be height rows in total. We...
September 6, 2022
Binary tree pruning
We will do a recursive DFS and check there the leaf node is 0 if 1, if it's 0, we remove it. Finally return the tree root.
Time Complexity: O(n)
Space Complexity: O(n)