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

Read

September 8, 2022

Build an array with stack operations

We will create a stack to match with our target and also create a result list. Then we start a loop from 1 to n and append each number to the stack. If the top of the stack does not match our target,...

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

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

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

Sort characters by frequency

We will first count the characters of the string and sort them by frequency. Then iterate over it, put the character as many time as it appear in the original string, then finally merge it together...

Read

September 6, 2022

Lexicographical numbers

We can just create a list of numbers from 1 to n, convert them to string and sort them. Time Complexity: O(nlog(n)) Space Complexity: O(1) Also, we can create a...

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