Problems
November 21, 2022
Nearest exit from entrance in maze
We will start from the entrance, run BFS through the matrix and find the nearest exit. If we can't find the exit, we will return -1
.
Time complexity: O(mn)
Space...
November 21, 2022
Number of operations to make network connected
We will create an adjacency list of the graph and then we will traverse the graph using DFS and count the number of connected components. If the number of connected components is greater than 1, then...
ReadNovember 21, 2022
Sum of absolute differences in a sorted array
The absolute difference between two numbers a and b is:
a - b
: When a is greater than or equal to b.
b - a
: When a is less than or equal to b.
These cases overlap when a...
November 21, 2022
Total cost to hire k workers
We will sort the workers by their quality and iterate through them. For each worker, we will add their wage to the heap and remove the highest wage if the heap size is greater than k
. We...
November 21, 2022
Vowel spellchecker
We will use a set to store the words in the wordlist. Then, we will use a dictionary to store the words in the wordlist with the vowels replaced with *
. Then, we will iterate through the...
November 20, 2022
All nodes distance k in binary tree
First we will traverse the tree using DFS and create an adjacency list for each node. Then we will use BFS to find all nodes that are k
distance away from the target node.
Time...
November 20, 2022
Find all duplicates in an array
We will count the frequency of each number in the array, and return the numbers that appear twice.
Time complexity: O(n)
Space complexity: O(n)
November 20, 2022
Maximum length of pair chain
We will first sort all the pairs by the second element. Then we will iterate over each pair and we will check if the current pair's first element is greater than the previous pair's second element....
ReadNovember 20, 2022
Number of orders in the backlog
We will use two heaps to keep track of the buy and sell orders. The buy orders will be stored in a max heap and the sell orders will be stored in a min heap. We will also keep track of the total...
ReadNovember 20, 2022
Number of ways to arrive at destination
We will use a priority queue to keep track of the minimum distance from the source to the destination. Then we will iterate over each node until the destination node, and we will check if the current...
Read