forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1246.cpp
More file actions
28 lines (28 loc) · 748 Bytes
/
Copy path1246.cpp
File metadata and controls
28 lines (28 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution
{
public:
int minimumMoves(vector<int>& arr)
{
int n = arr.size();
int mem[n + 1][n + 1] = {};
for (int l = 1; l <= n; l++)
{
int i = 0, j = l - 1;
while (j < n)
{
if (l == 1) mem[i][j] = 1;
else
{
mem[i][j] = mem[i + 1][j] + 1;
for (int k = i + 1; k <= j; k++)
{
if (arr[k] == arr[i])
mem[i][j] = min(mem[i][j], mem[i + 1][k - 1] + mem[k + 1][j] + (i + 1 == k));
}
}
++i, ++j;
}
}
return mem[0][n - 1];
}
};