Problems
December 23, 2022
Convert binary search tree to sorted doubly linked list
We do an inorder traversal and make an arr of sorted nodes. We then create the doubly linked list by iterating through the array.
Time complexity: O(n)
Space complexity:...
December 23, 2022
Flatten a multilevel doubly linked list
We will use a recursive DFS to solve this problem. We will iterate over the head
linked list and for each node, we will check if the node has a child. If it does, we will recursively...
December 22, 2022
Sum of distances in tree
We will create a tree from the edges. We will create a sums
array to store the sum of distances from each node to all other nodes. We will create a counts
array to store the...
December 22, 2022
Largest values from labels
We will use a greedy approach to solve this problem. We will sort the values in descending order and then iterate over the values. We will keep track of the number of items with each label. We will...
ReadDecember 22, 2022
Check if there is a path with equal number of 0s and 1s
We will use top-down dynamic programming to solve this problem. We will create a dfs
function to find if there is a path from the current node to the end node. We will return the result...
December 21, 2022
Possible bipartition
We will create an adjacency list from the dislikes list. Then we start traversing the graph with BFS, and for each alternate level, we will assign one color. If two adjacent node has the same color,...
ReadDecember 21, 2022
Largest merge of two strings
We will iterate through the two strings and compare the current character of each string. If the current character of word1
is greater than the current character of word2
,...
December 21, 2022
Find distance in a binary tree
We will use a recursive function to find the distance between two nodes. The function will return the distance between the two nodes if both nodes are in the tree. Otherwise, it will return...
ReadDecember 21, 2022
Maximal network rank
We will create an adjacency list to represent the graph. We will iterate through all pairs of cities and find the number of common neighbors. If the number of common neighbors is greater than the...
ReadDecember 20, 2022
Minimum initial energy to finish tasks
We will sort the tasks by the difference between the cost and the energy. We will iterate through the tasks and keep track of the current energy. If the current energy is less than the cost of the...
Read