Problems


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

Read

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

Read

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

Read

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

Read

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

Read

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

Read

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

Read

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

Read

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)

Read

September 6, 2022

Design a stack with increment operation

We will create a stack and store the max length of the stack. If someone push anything, if the stack is not already full, we will push it to the stack. Similarly, is the stack is not empty, we...

Read
... 60 61 62 63 64 ...