Problems


August 7, 2023

Lowest common ancestor of a binary tree IV

We will use a recursive approach to solve this problem. We will traverse the tree in a post-order fashion. We will return the node if it is equal to any of the nodes in the list of nodes. We will...

Read

July 24, 2023

Split strings by separator

We will iterate over the words and split them by the separator. We will then append the non-empty words to the result list. Time Complexity: O(n) where n is the number of...

Read

June 17, 2023

Number of increasing paths in a grid

We will start DFS from the top left corner of the grid and keep track of the number of paths that we have found so far. We will also keep track of the number of paths that we have found so far for...

Read

June 14, 2023

Missing ranges

We will add 2 numbers to the existing array lower-1 and upper+1 which will be the lower and upper bound of the range to avoid edge cases. Then we will iterate over the array...

Read

June 13, 2023

Check if all characters have equal number of occurrences

We will count all the occurance of the characters in the string and store it in a hashmap. Then we will check if all the values in the hashmap are equal or not. Time Complexity: O(n)...

Read

June 11, 2023

Summary ranges

We will iterate over the array and find the ranges. Then we will map the ranges to the required format. Time complexity: O(n) Space complexity: O(n)

Read

June 9, 2023

Maximum value at a given index in a bounded array

We can use binary search to find the maximum value at a given index in a bounded array. We can use the formula for the sum of an arithmetic series to find the maximum value at a given index. The...

Read

June 7, 2023

Check if a number is majority element in a sorted array

We can just count the number of times target appears in nums. If it appears more than len(nums)//2 times, then it is the majority element. Time complexity:...

Read

June 7, 2023

Count negative numbers in a sorted matrix

We will iterate through each row and count the number of negative numbers in each row. We will add the count to the total count. Time complexity: O(n) Space complexity:...

Read

June 5, 2023

Can make arithmetic progression from sequence

We will sort the array and then check if the difference between each element is the same. Time complexity: O(nlog(n)) Space complexity: O(1)

Read
1 2 3 4 5 ...