Problems
September 6, 2022
Seat reservation manager
We will create a min heap with the capacity of the seat manager. Then when someone reserve a seat, we will pop it from our min heap, and when someone unreserve a seat, we will push back the value to...
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...
ReadSeptember 6, 2022
Sort an array
We will use divide and conquar to solve this sorting porblem. We will be using classic merge sort.
Time Complexity: O(nlog(n))
Space Complexity: O(nlog(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 5, 2022
All ancestors of a node in a directed acyclic graph
First we will create an ancestors adgacency list, where we can get the immediate parent of each node from the edge list. Then we will run DFS from each node, and get the node list of all reachable...
ReadSeptember 5, 2022
Basic calculator II
We will take a stack. Initially our operation and operand will be 0 and +
append each character to the stack. If the character is numeric, until we reach an operand we will calculate the...
September 5, 2022
Basic calculator III
We will take a stack. Initially our operation and operand will be 0 and +
append each character to the stack. If the character is numeric, until we reach an operand we will calculate the...
September 5, 2022
Basic calculator
We will take a stack. Initially our operation and operand will be 0 and +
append each character to the stack. If the character is numeric, until we reach an operand we will calculate the...
September 5, 2022
Battleships in a board
We will run DFS to explore all the values after we found one X
and mark that as visited and count the number of battleship by 1. After traverse the whole board, we will return the count...
September 5, 2022
Count primes
We will use the algorithm Sieve of Eratosthenes to find the number of primes within a range.
Time Complexity: O(nlog(log(n)))
Space Complexity: O(n)