Problems


November 28, 2022

Binary tree vertical order traversal

We will use BFS to traverse the tree. We will keep track of the horizontal distance of each node from the root. We will use a dictionary to store the nodes at each horizontal distance. We will use a...

Read

November 28, 2022

Count pairs in two arrays

nums1[i] + nums1[j] > nums2[i] + nums2[j], i < j can be converted to (nums1[i]-nums2[i]) + (nums1[j]-nums2[j]) > 0, i < j. At this point, we will realize that...

Read

November 28, 2022

Find players with zero or one losses

We use 3 different hashset to store zero losses, one loss and more than one loss. Then we iterate through the results array and update the hashsets accordingly. Finally, we iterate...

Read

November 28, 2022

Remove zero sum consecutive nodes from linked list

We will keep track of the prefix sum of the linked list. If the prefix sum is repeated, we will remove the nodes between the repeated prefix sum and the current prefix sum. Time complexity:...

Read

November 28, 2022

Minimize product sum of two arrays

We will sort the two arrays, first one in ascending order and second one in descending order. That will make sure the product of both array's item will be minimized. Then we will iterate over the...

Read

November 28, 2022

Validate binary tree nodes

We will first find the root of the tree. Then we will traverse the tree and check if we can reach all the nodes from the root. We will use BFS to traverse. If we can reach all the nodes, then the...

Read

November 28, 2022

Design hashset

We will use a boolean array to store the values. We will use the value as the index of the array and set the value to true if the value is added and false if the value is...

Read

November 27, 2022

Binary tree upside down

We will use recursive DFS to solve the problem. We will recursively call the function on the left child of the root. Then we will set the left child of the root to the right child of the root. Then...

Read

November 27, 2022

Arithmetic slices II subsequence

We will use dynamic programming to solve the problem. We will use a hashmap to store the number of arithmetic slices that ends with the current number. We will iterate over the numbers, and for each...

Read

November 27, 2022

Arithmetic slices

We will take a top-down approach to solve the problem. Then we memoize each subproblem to avoid duplicate computation. Time complexity: O(n), n is the length of nums Space...

Read
... 28 29 30 31 32 ...