Problems
September 6, 2022
H-index II
We will seach for the highest mid where the value of mid is greater that or equal to the numbers of elements on the right from that mid index.
Time Complexity: O(log(n))
Space...
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...
ReadSeptember 6, 2022
Implement magic dictionary
We will use the Trie data structure to store all the elements in the dictionary. Building the dictionary is pretty strait forward, we will insert all the words from the dictionary to the trie. As we...
ReadSeptember 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
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
Merge nodes in between zeros
We will create a dummy node for our result. Then we will start from the head, sum the values of the nodes until we reach another 0, then attach a node to our result list. We will repeat it until we...
ReadSeptember 6, 2022
Query kth smallest trimmed number
We will take the nums, iterate over each query, and take the last trim values of the nums and store it as integer in a values array along with the index. Then we sort this values array and take the...
ReadSeptember 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
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
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:...