Ctrl K

Erase Mirror Blocks

Hard
You are given an integer array 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.
Example 1
values=[2, 5, 7, 5, 3] → 3

Erase [5, 7, 5] in one move, then [2] and [3]: 3 moves in total.

Example 2
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.

Example 3
values=[8, 3] → 2

The two numbers differ, so each needs its own move.

Constraints
  • 1 ≤ len(values) ≤ 100
  • 1 ≤ values[i] ≤ 20
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWater Between Walls HardBiggest Solid Panel Block HardProfit From Two Trades HardEvery Way to Space the Words HardCrossing the Cave Hard
loading Python…
Input
values=[2, 5, 7, 5, 3]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 101 tests on the server, hidden ones included.