-
Notifications
You must be signed in to change notification settings - Fork 481
/
1246.go
36 lines (35 loc) · 885 Bytes
/
1246.go
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
func minimumMoves(arr []int) int {
n := len(arr)
mem := make([][]int, n + 1)
for i := 0; i <= n; i++ {
mem[i] = make([]int, n + 1)
}
for l := 1; l <= n; l++ {
i, j := 0, l - 1
for j < n {
if l == 1 {
mem[i][j] = 1 + mem[i + 1][j]
} else {
mem[i][j] = 1 + mem[i + 1][j]
for k := i + 1; k <= j; k++ {
if arr[k] == arr[i] {
tmp := mem[i + 1][k - 1] + mem[k + 1][j]
if i + 1 == k {
tmp++
}
mem[i][j] = min(mem[i][j], tmp)
}
}
}
i++
j++
}
}
return mem[0][n - 1]
}
func min(a, b int) int {
if a < b {
return a
}
return b
}