Questions 1 and 2 were easy, two array questions.
Question 3 was kind of like a match-three game — a matrix where different numbers represent different colors. If a cell has two neighbors (up, down, left, or right) that are the same color as it, they explode and get cleared. After the whole matrix finishes clearing, the cells that weren't cleared fall down. Empty cells are represented by 0. Return the matrix after one round of clearing. A brute force solution passed all the test cases.
I didn't solve question 4. The gist of it: there's an array where each number represents a type of fruit, and you're given a number k representing the minimum number of matching-fruit pairs a subarray needs to have, and you need to find all such subarrays. For example [0, 1, 0, 1, 0], k=2, should return 3, because only [0, 1, 0, 1], [1, 0, 1, 0], and [0, 1, 0, 1, 0] satisfy the condition. As long as there are two of the same fruit anywhere in the window it counts as one pair — more than two of the same fruit still only counts as one pair. I struggled with a sliding window approach for half an hour and still couldn't get it, so in the end I just submitted a brute force solution.
Discussion
Loading comments…