Problems


August 28, 2022

Combinations

This is classic backtracking problem. For a given combination of values, you want only 1 tuple. You can achieve this by making sure that for 3 values a, b, c, that a < b < c. I implement this...

Read

August 28, 2022

Validate stack sequences

We will pushed each element from the pushed to a stack, and check if the top of the element is equal to the first element of popped, if yes, we pop the element from the stack. After we are done with...

Read

August 28, 2022

Game of life

We will create a copy of the given matrix, also create a helper function to count the live neighbors. Then, we iterate over each element of the board, count it's live neighbors, then change the value...

Read

August 28, 2022

Sort the matrix diagonally

We will create a lehper function, that will take all the diagonal element from the matrix, based on a given row, column position, sort that and then replace the original matrix values with the sorted...

Read

August 28, 2022

Unique paths II

We will first solve it with brute force using recursion and then use memoization to make it efficient. For recursion, we can think about the base case, if our current position is out of bound or it...

Read

August 28, 2022

First missing positive

We will use our input array as the hashset, so that we can solve the problem in constant space. First, we will iterate over the input, and mark each negative number to 0, as it doesn't have any...

Read

August 27, 2022

Interval list intersections

We will iterate over both list and check whether we have any overlaps or not, if we have we take the maximum of start time and minimum of end time as the intersection value and append it to our...

Read

August 27, 2022

3sum closest

We have already sorted the 3 sum problem, it is basically the same problem with a little twist. First we sort the array, then we take the first element at first position, then for the rest of the...

Read

August 27, 2022

Number of smooth descent periods of a stock

We will start by result and count to be equals to 1. Then in each iteration of the array from index 1 to the rest, we check with the previous element, if the previous number is exactly bigger by 1,...

Read

August 27, 2022

Swap nodes in pairs

We will create a dummy node, and set this as previous node, assing next pointer to head. Then from head, we take 2 nodes each time, swap their positions, then move the current and previous node until...

Read
... 70 71 72 73 74 ...