Problems


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 28, 2022

Design phone directory

We will use a set to keep track of the available numbers. We will also use a queue to keep track of the numbers that have been released. Time complexity: O(1) Space complexity:...

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

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

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

Shortest word distance

We will keep track of both words' last seen index. Then we will iterate through the words array and update the result if we find either of the words. Finally, we will return the result....

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 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

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
... 28 29 30 31 32 ...