Problems


July 24, 2022

Search a 2d matrix

We can just consider the matrix as a linear sorted array. Then if we need an element number i in our flattened list, we can get it by matrix[i//col][i%col]. Then we can do a regular binary search on...

Read

July 24, 2022

Search a 2d matrix II

if the current grid value matrix[r][c] is less than the target, that means, we don't need to search in this row anymore, as all the values are already bigger than the matrix[r][c]. If the value is...

Read

July 24, 2022

Implement queue using stacks

We will have 2 stack, one for input, one for output. Whenever we push anything, we push it to the input stack, then pop every element from input stack and push it back to output stack. Every other...

Read

July 24, 2022

Permutations II

First we will count the number of each element, then we create our decision tree, if we take one element, then we remove that element form our count hashmap. If it has multiple instance, we reduce...

Read

July 24, 2022

Longest substring without repeating characters

We will use a set to keep track of repeating characters. We will have 2 pointers, we will move our right pointer, check if the character as right pointer is already exist in the set, if exists, then...

Read

July 24, 2022

Subsets II

We will first sort the element. Then like the original subset problem, we will have 2 choice for each element, either choose it or skip it. As we don't want duplicate subset, we will skip the same...

Read

July 23, 2022

Design add and search words data structure

We will implement the trie as normal, the addWord function will be identical to a normal trie. The search function will be a little different. We will iterate over each character, if the character is...

Read

July 23, 2022

3Sum

If you already solved the 2 sum ii problem, you might get the idea. First we will sort the list. Then we take the first element at first position, then for the rest of the elements we take 2...

Read

July 23, 2022

Subsets

We will do backtracking to solve the result. For each element of the list, we have 2 option, either we take the element or we leave the element. From this base case, if we run dfs for from the...

Read

July 23, 2022

Task scheduler

We will first count each characters and create a max heap out of it. Then we pop the item from the heap, put it on a queue, and increase the value of our counter. Then we process the task, if there...

Read
... 85 86 87 88 89 ...