Problems


October 9, 2022

Frequency of the most frequent element

First we will sort the array. Then it turns into a sliding window problem, the key is to find out the valid condition k + sum >= size * max. For every new element nums[r] to the sliding window, add...

Read

October 9, 2022

Flip equivalent binary trees

We will start traversing both tree with DFS and check whether the left subtree and right subtree are equal for both tree or left subtree is equal to right subtree and vice-versa both tree. Time...

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

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

Length of last word

We will first start from the end and move the end pointer till we find a character. Then we take another pointer beginning from the end character and move towards the beginning of the string until we...

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