-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path1246-palindrome-removal.js
More file actions
47 lines (41 loc) · 1.18 KB
/
1246-palindrome-removal.js
File metadata and controls
47 lines (41 loc) · 1.18 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 1246. Palindrome Removal
* https://leetcode.com/problems/palindrome-removal/
* Difficulty: Hard
*
* You are given an integer array arr.
*
* In one move, you can select a palindromic subarray arr[i], arr[i + 1], ..., arr[j]
* where i <= j, and remove that subarray from the given array. Note that after removing
* a subarray, the elements on the left and on the right of that subarray move to fill
* the gap left by the removal.
*
* Return the minimum number of moves needed to remove all numbers from the array.
*/
/**
* @param {number[]} arr
* @return {number}
*/
var minimumMoves = function(arr) {
const n = arr.length;
const dp = new Array(n).fill().map(() => new Array(n).fill(Infinity));
for (let i = 0; i < n; i++) {
dp[i][i] = 1;
}
for (let length = 2; length <= n; length++) {
for (let i = 0; i <= n - length; i++) {
const j = i + length - 1;
if (arr[i] === arr[j]) {
if (length === 2) {
dp[i][j] = 1;
} else {
dp[i][j] = dp[i + 1][j - 1];
}
}
for (let k = i; k < j; k++) {
dp[i][j] = Math.min(dp[i][j], dp[i][k] + dp[k + 1][j]);
}
}
}
return dp[0][n - 1];
};