Problems
November 16, 2022
Making file names unique
We will use a hashmap to store the count of each file name. We will iterate over all the file names and if the file name is not present in the hashmap, we will add it to the result. If the file name...
ReadNovember 16, 2022
Minimum time visiting all points
If we go diagonally, we can reach the destination in the minimum time, actually twice as first as moving vertically and horizontally. So, we will find the maximum of the difference between the x and...
ReadNovember 16, 2022
Delete nodes and return forest
We will use DFS for deleting the nodes. We recursively delete the left and right subtrees and then return the root. If the root is in the set, we return the left and right subtrees, and if the root...
ReadNovember 16, 2022
Path in zigzag labelled binary tree
We will use DFS for finding the path. We recursively find the path in the left and right subtrees and then return the root. If the root is in the set, we return the left and right subtrees, and if...
ReadNovember 16, 2022
Construct binary search tree from preorder traversal
As preorder traversal is given, we know the first element will be the root. We will create a function witg a bound the maximum number it will handle. The left recursion will take the elements smaller...
ReadNovember 16, 2022
Minimum distance to the target element
We will iterate over all the elements and find the minimum distance.
Time complexity: O(n)
Space complexity: O(1)
November 15, 2022
Dungeon game
We will solve the problem recursively and then memoize the result to avoid duplicate computation. For base case, if the knight is at the bottom right corner, then the minimum health required is 1. If...
ReadNovember 15, 2022
Find duplicate subtrees
We will use DFS and do preorder traversal to find all the subtrees. Then we can use a hash map to store the frequency of each subtree. If the frequency of a subtree is greater than 1, then we can add...
ReadNovember 15, 2022
Count of matches in tournament
We can simply count the number of matches played by counting the number of times we divide the number of teams by 2. We can also count the number of teams by counting the number of times we multiply...
ReadNovember 15, 2022
Find mode in binary search tree
We will use a hashmap to store the frequency of each node's value. We will then traverse the hashmap and return the keys with the maximum frequency.
Time complexity: O(n)
Space...