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 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 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
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
Shortest subarray to be removed to make array sorted
We will use two pointers to find the longest subarray that is sorted. The answer will be the length of the array minus the length of the longest subarray.
Time complexity: O(n)
Space...
November 16, 2022
Trim a binary search tree
We will use DFS for trimming. We recursively trim the left and right subtrees and then return the root. If the root is less than low
, we return the right subtree, and if the root is...
November 15, 2022
Binary tree tilt
We will use DFS to find the tilt of each subtree and then add them together.
Time complexity: O(n)
Space complexity: O(n)
November 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
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...
Read