Problems
May 7, 2023
Matrix diagonal sum
We will traverse the matrix row by row. For each row, we have to take 2 diagonal position and add it to the result. If the matrix has an odd number of rows, we have to subtract the middle element...
ReadMay 6, 2023
Concatenation of array
We can just merge the array with itself and return.
Time complexity: O(n)
Space complexity: O(1)
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...
ReadMay 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...
ReadMay 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...
ReadMay 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:...
ReadMay 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...
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:...
ReadFebruary 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...
ReadFebruary 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...