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...
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:...
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...
ReadSeptember 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...
ReadSeptember 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,...
ReadSeptember 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...
ReadSeptember 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)
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...
ReadSeptember 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