values. In one move you may pick a contiguous block of the array that reads the same forwards and backwards (for example [3], [2, 2] or [5, 1, 5]) and erase it. The elements on both sides of the erased block then close the gap and become neighbours.
Return the minimum number of moves needed to erase the whole array.values=[2, 5, 7, 5, 3] → 3
Erase [5, 7, 5] in one move, then [2] and [3]: 3 moves in total.
values=[4, 1, 6, 1, 9, 4] → 2
Erase [1, 6, 1] first; what remains is [4, 9, 4], which goes in the second move. Answer: 2.
values=[8, 3] → 2
The two numbers differ, so each needs its own move.
values=[2, 5, 7, 5, 3]
3
Run checks the first 3 tests in your browser. Submit checks all 101 tests on the server, hidden ones included.