Problems


May 7, 2023

Sort array by increasing frequency

We will use a hashmap to store the frequency of each number. Then we will sort the array by the frequency of each number. If the frequency of two numbers is the same, we will sort them by the value...

Read

May 6, 2023

Find the longest valid obstacle course at each position

Given an array of integers, find the longest non-decreasing subsequence. This problem is very similar to Longest increasing subsequence. We will solve this with a greedy approach. The key is, the...

Read

May 6, 2023

Find score of an array after marking all elements

We will use a heap to store the elements. We will pop the maximum element from the heap and mark it. Then we will push the maximum element divided by 2 back to the heap. We will repeat this process...

Read

May 6, 2023

Concatenation of array

We can just merge the array with itself and return. Time complexity: O(n) Space complexity: O(1)

Read

May 5, 2023

Substrings that begin and end with the same letter

Traverse from left to right, when visiting a character C, number of substring that begin and end with C in the prefix substring is the frequency of C (in this prefix substring)+1. Time complexity:...

Read

May 5, 2023

Maximum number of vowels in a substring of given length

We will first count the number of vowels in the whole string. Then we will use a sliding window to find the maximum number of vowels in a substring of given length. We will return the result. Time...

Read

May 4, 2023

Maximum sum with exactly k elements

We will pick up the maximum number and add it to the result. Then for each iteration of k, we add 1 to the maximum number and add it to the result. We will return the result. Time complexity:...

Read

May 4, 2023

Index pairs of a string

We will first create a set of all the words in words. Then, we will iterate through text and check if the current word is in the set. If it is, we will add the current index...

Read

February 1, 2023

Sentence similarity

The bruteforce way is to everytime when you see a word in sentence2, you loop through the entire similarPairs, see if the word exisits in one of the paris together with the corresponding word in...

Read

February 1, 2023

Greatest common divisor of strings

We will check if the length of str1 is divisible by the length of str2. If it is, we will check if str1 is equal to str2 multiplied by the length...

Read
... 4 5 6 7 8 ...