Problems


November 1, 2022

Minimum absolute difference in bst

We will traverse the tree with inorder traversal, that way we will get a sorted array. We will keep track of the minimum difference between two consecutive elements in the array, return it as the...

Read

November 1, 2022

Minimum swaps to group all 1s together II

We will count number of ones in nums, let the number of ones be stored in the variable ones. Append nums to nums because we have to look at it as a circular array. Find the maximum...

Read

November 1, 2022

Where will the ball fall

We drop the ball at grid[0][c] and track ball position c1, which initlized as c. An observation is that, if the ball wants to move from c1 to c2, we must have the board direction grid[r][c1] ==...

Read

October 31, 2022

Find all possible recipes from given supplies

We will create a graph where each node represents a recipe and each edge represents a supply. We will iterate over the recipes and add the edges to the graph. We will use topological sort to solve...

Read

October 31, 2022

Optimal division

Regardless of parentheses, every element is either in the numerator or denominator of the final fraction. The expression nums[0] / ( nums[1] / nums[2] / ... / nums[n-1] ) has every element in the...

Read

October 31, 2022

Pairs of songs with total durations divisible by 60

Calculate the time % 60 then it will be exactly same as two sum problem. Time complexity: O(n) Space complexity: O(n)

Read

October 31, 2022

Toeplitz matrix

We will iterate over the rows and columns and compare diagonally adjacent elements. If they are not equal, we can return false. If we reach the end of the loop, we can return true. Time...

Read

October 30, 2022

Shortest path in a grid with obstacles elimination

We will use a queue to store the nodes that we need to visit. We will also use a set to store the nodes that we have already visited. We will traverse the grid in a breadth-first manner. If the...

Read

October 30, 2022

Divide array into equal pairs

We can use a hashmap to store the frequency of each element in the array. Then we can iterate over the array and check if the frequency of the current element is even or not. If it is even, we can...

Read

October 30, 2022

Valid number

We cal also use 3 flags for met_dot, met_e and met_digit. We can iterate over the string and update the flags. If the current character is a digit, we can set met_digit to true. If the current...

Read
... 43 44 45 46 47 ...