-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1246.cpp
26 lines (25 loc) · 917 Bytes
/
1246.cpp
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
class Solution {
public:
int minimumMoves(vector<int>& arr) {
int n = arr.size();
vector<vector<int>> dp(n, vector<int>(n, n));
for (int i = 0; i < n; ++i) dp[i][i] = 1;
for (int i = 0; i < n - 1; ++i) {
if (arr[i] == arr[i + 1]) dp[i][i + 1] = 1;
else dp[i][i + 1] = 2;
}
for (int len = 3; len <= n; ++len) {
for (int left = 0, right = left + len - 1; right < n; ++left, ++right) {
if (arr[left] == arr[right]) {
dp[left][right] = min(dp[left][right], dp[left + 1][right - 1]);
}
else {
for (int mid = left; mid < right; ++mid) {
dp[left][right] = min(dp[left][right], dp[left][mid] + dp[mid + 1][right]);
}
}
}
}
return dp[0][n - 1];
}
};