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

Read

September 3, 2022

Gray code

There is formula to get Gray's code, for every number i, it i ^ (i>>1). We can use the foumula to get the values for 2^n numbers and return it as the result. Time Complexity:...

Read

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

Read

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

Read

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

Read

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

Read

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

Read

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

Read

September 2, 2022

Sudoku solver

We will use 3 hashset to store the value of rows, columns and boxes. For rows and columns hashset we will use the row and column number as key. For square hashset, we can use the integer division of...

Read

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
... 66 67 68 69 70 ...