Problems
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...
ReadAugust 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...
ReadAugust 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...
ReadAugust 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...
ReadAugust 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...
ReadAugust 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...
ReadAugust 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,...
ReadAugust 27, 2022
Remove k digits
We will use a monotonic decreasing stack to keep track of the digits. We will remove most significant k digits from the stack and then return the values, if the stack is empty at that point we will...
ReadAugust 27, 2022
Check if a string contains all binary codes of size k
First we will divide the string to all possible substring of length k. Then add all of them into a hashset. If the length of the hashset is equals to 2^k, then we found all possible number and return...
ReadAugust 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