Problems


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

September 6, 2022

Reverse words in a string

We will strip the extra space and then split the string with space. Finally, we reverse the words, combine them to a string and return. Time Complexity: O(n) Space Complexity:...

Read

September 6, 2022

Remove stones to minimize the total

We will use a max heap to get the top k elements from the piles. As python does not have max heap out of the box, we can multiply each element by -1 and thus min heap will work as max heap. Now we...

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

September 6, 2022

Replace words

We will use a prefix tree or trie with our dictionary. Then for each word in the sentence, we will look for the root of the word in the trie, if we found something, we will replace it with our root,...

Read

September 6, 2022

Linked list random node

When we initialize the solution class, we will traverse the whole list and store it's values to a list. Then each time we are asked to get a random number, we will use random module to choose a value...

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

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

Smallest number in infinite set

We will first create a heap with 1000 elements from 1 to 1000 as at most 1000 call will be made. If we want to add something, we will check whether it is already present in the heap, if not, we will...

Read

September 6, 2022

H-index

We will use count sort to sort the elements in linear time and then search for the highest value, where numbers of elements after that index is greater than or equal to that value. Time...

Read
... 61 62 63 64 65 ...