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

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

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

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

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

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

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

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

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

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
... 61 62 63 64 65 ...