Problems


October 9, 2022

Guess number higher or lower

This is a classic binary search problem. We will use binary search to find the number. We will use provided guess function to check whether the number is equal to the target or not....

Read

October 9, 2022

Two sum IV input is a bst

This is very similar to two sum problem. But rather than traversing an array, we will traverse a tree. We will be using DFS to traverse the tree and along with the way, we will store the difference...

Read

October 8, 2022

Find all anagrams in a string

We will use counter to calculate the number of characters of string p. Then we take a sliding window of lenght p, then compare the character count with the character count of p. If we found a match,...

Read

October 8, 2022

Find the index of the first occurrence in a string

We will create a substring of length of the needle and compare it with the needle, if we find a match, we return the index as result, otherwise return -1. Time Complexity: O(n) Space...

Read

October 8, 2022

Remove element

We will take a pointer at the beginning of the array, then iterate over the whole array. If the value doesn't match the given values, we assing it to the pointer's position of the array and then...

Read

October 8, 2022

Unique length 3 palindromic subsequences

For each palindromes in format of "aba", we enumerate the character on two side. We find its first occurrence and its last occurrence, all the characters in the middle are the candidate for the...

Read

October 7, 2022

Reverse linked list II

First if the left and right position of the list is same, we can just return the list. Otherwise, we will take a pointer, traverse till the left position, then reverse the list in place till the...

Read

October 7, 2022

Search insert position

This one is the classic binary search problem. We will strat looking for the target, if we find the target then we insert on that position. If we don't find the target, then we insert at the last low...

Read

October 7, 2022

Single threaded cpu

First we sort the tasks according to start time, remember to keep a reference to the original task index. Set the current time to the first start time in the task list. Push all tasks whose start...

Read

October 6, 2022

Baseball game

We will use a stack to add the input and from there we will just follow the problem statement instruction. Finally we will return the sum of stack itself as result. Time Complexity:...

Read
... 50 51 52 53 54 ...