Problems
November 18, 2022
First letter to appear twice
We will use a hashset to keep track of the letters we have seen. If we see a letter that is already in the hashset, we return it. Otherwise we add it to the hashset. Time complexity:...
ReadNovember 17, 2022
Find the highest altitude
We will iterate over the gain array and add the gain to the current altitude. We will update the maximum altitude if the current altitude is greater than the maximum altitude. We can also achieve...
ReadNovember 17, 2022
Finding the users active minutes
We will use a hashset to count the unique logs for each user. Then we iterate over that, take the length of the hashset and add it to the result.
Time complexity: O(n)
Space...
November 17, 2022
Rectangle area
We will find the area of the first rectangle and the second rectangle. We will find the area of the overlapping rectangle by finding the length and breadth of the overlapping rectangle. We will...
ReadNovember 17, 2022
Number of digit one
We will create a function countDigitOne
which will take a number as input. We will initialize a variable count
to 0
. We will initialize a variable...
November 17, 2022
Symmetric tree
We will create a function isMirror
which will take two nodes as input. If both the nodes are None
, we will return True
. If one of the nodes is...
November 16, 2022
Insert into a binary search tree
We will use DFS for inserting. We recursively insert the left and right subtrees and then return the root. If the root is null, we return the new node. If the root is greater than the value, we...
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
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...