Problems


October 4, 2022

N-queens II

We will have 3 different sets, one for the column, one for positive diagonal and one for negative diagonal. We will iterate over the each row, and try to add the queen in the board. If this is...

Read

October 4, 2022

Ugly number

If the number is negative, then it's not a ugly number. Then we do exactly what the problem statement says, we divide the number by 2, 3 and 5 until we can't divide it any more with these numbers. If...

Read

October 3, 2022

Asteroid collision

We will take a stack and iterate over all the elements. If the element is positive, we append it on the stack. Else we take the number, check the value of the number. If it is equal to the top of the...

Read

October 3, 2022

Minimum time to make rope colorful

For a group of continuous same characters, we need to delete all the group but leave only one character. For each group of continuous same characters, we need cost = sum_cost(group) -...

Read

October 2, 2022

Rotate array

This approach is based on the fact that when we rotate the array k times, k%nk elements from the back end of the array come to the front and the rest of the elements from the front shift backwards....

Read

October 2, 2022

Number of dice rolls with target sum

We will take the brute force method to solve the problem, in every iteration till n, we will take 1 to k elements and add it to our total recursively. Then when we reach the target, we will add it to...

Read

October 2, 2022

Minimum difference between highest and lowest of k scores

We will first sort the elements and then loop over all the elements and take the difference between k elements, and keep a running minimum and then return that as result. Time Complexity:...

Read

October 2, 2022

Move zeroes

We will take a simple two pointers approach, the left will be the first index. Then we iterate over the array, and every time we find a zero, we change the position of it with the next character...

Read

October 1, 2022

Valid palindrome II

We will take two pointers, one at the beginning and one at the end. Then we start compare, if it doesn't match, then we try to skip the character in the left pointer and character in right pointer...

Read

October 1, 2022

Reverse string

We will take 2 pointers, one at the beginning and one at the end of the string. Then we swap their position and move to the next character. Once these two pointers meet, we stop. Time Complexity:...

Read
... 52 53 54 55 56 ...