Problems
September 3, 2022
Minimum moves to equal array elements II
We will first take the median of the array by sorting it and take the middle value, this will be our target. Then we take the difference from each number to the target, add that up and return that as...
ReadSeptember 3, 2022
Decode string
We will use a stack and append the values until we reach ]
. Then we pop the values from stack and make a string until we reach [
. Then we pop the numbers from the stack and...
September 3, 2022
Balance a binary search tree
We will first traverse the tree with inorder traversal, and append all the values to an array. Then we create a binary search tree with the values, where root will be the middle of the array, thus we...
ReadSeptember 3, 2022
Largest number
We will take the input array, and sort it with our custom compare method, where we sort it in natural string order in reverse. Then we convert the the numbers into string and merge it together to get...
ReadSeptember 3, 2022
Sell diminishing valued colored balls
One naive approach is to repeat for orders times, and each time, take the largest one, decrease it and place back to the array. This will take O(orders * logN) time complexity if using heap. Let's...
ReadSeptember 3, 2022
Equal row and column pairs
We will create a hashmap where we will count the rows occurance, the key will be the row as tuple and the value will be the count. Then we move through the column, search in the lookup hashmap for...
ReadSeptember 3, 2022
Design underground system
We will use 2 hashmap to store the passengers data and travel time data. When someone check if, we add it to the passengers hashset, along with the time. When someone check out, we take the start...
ReadSeptember 2, 2022
Reachable nodes with restrictions
We will create an adjacency list from the edges list. Then we convert our restricted list to a set for constant lookup. Then we perform a BFS to visit all the nodes, if the node is already been...
ReadSeptember 2, 2022
Average of levels in binary tree
We will run BFS and after traversal of each level, we append the average in our result, and finally return it after the end of traversal.
Time Complexity: O(n)
Space Complexity:...
September 2, 2022
Minimum time difference
First we convert the hours into minutes and append them into a new array. Then we sort the new array and go through each element and look for minimum difference. Once we reach the last element in the...
Read