publicintmaxCoins(int[] nums) { intn= nums.length; val = newint[n + 2]; for (inti=1; i <= n; i++) { val[i] = nums[i - 1]; } val[0] = val[n + 1] = 1; rec = newint[n + 2][n + 2]; for (inti=0; i <= n + 1; i++) { Arrays.fill(rec[i], -1); } return solve(0, n + 1); }
publicintsolve(int left, int right) { if (left >= right - 1) { return0; } if (rec[left][right] != -1) { return rec[left][right]; } for (inti= left + 1; i < right; i++) { intsum= val[left] * val[i] * val[right]; sum += solve(left, i) + solve(i, right); rec[left][right] = Math.max(rec[left][right], sum); } return rec[left][right]; } }
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution: defmaxCoins(self, nums: List[int]) -> int: n = len(nums) val = [1] + nums + [1] @lru_cache(None) defsolve(left: int, right: int) -> int: if left >= right - 1: return0 best = 0 for i inrange(left + 1, right): total = val[left] * val[i] * val[right] total += solve(left, i) + solve(i, right) best = max(best, total) return best
funcmaxCoins(nums []int)int { n := len(nums) val := make([]int, n + 2) for i := 1; i <= n; i++ { val[i] = nums[i - 1] } val[0], val[n+1] = 1, 1 rec := make([][]int, n + 2) for i := 0; i < len(rec); i++ { rec[i] = make([]int, n + 2) for j := 0; j < len(rec[i]); j++ { rec[i][j] = -1 } } return solve(0, n + 1, val, rec) }
funcsolve(left, right int, val []int, rec [][]int)int { if left >= right - 1 { return0 } if rec[left][right] != -1 { return rec[left][right] } for i := left + 1; i < right; i++ { sum := val[left] * val[i] * val[right] sum += solve(left, i, val, rec) + solve(i, right, val, rec) rec[left][right] = max(rec[left][right], sum) } return rec[left][right] }
funcmax(x, y int)int { if x > y { return x } return y }
int rec[502][502]; int val[502]; intsolve(int left, int right) { if (left >= right - 1) { return0; } if (rec[left][right] != -1) { return rec[left][right]; } for (int i = left + 1; i < right; i++) { int sum = val[left] * val[i] * val[right]; sum += solve(left, i) + solve(i, right); rec[left][right] = fmax(rec[left][right], sum); } return rec[left][right]; }
intmaxCoins(int* nums, int numsSize) { memset(rec, -1, sizeof(rec)); val[0] = val[numsSize + 1] = 1; for (int i = 1; i <= numsSize; i++) { val[i] = nums[i - 1]; }
funcmaxCoins(nums []int)int { n := len(nums) rec := make([][]int, n + 2) for i := 0; i < n + 2; i++ { rec[i] = make([]int, n + 2) } val := make([]int, n + 2) val[0], val[n+1] = 1, 1 for i := 1; i <= n; i++ { val[i] = nums[i-1] } for i := n - 1; i >= 0; i-- { for j := i + 2; j <= n + 1; j++ { for k := i + 1; k < j; k++ { sum := val[i] * val[k] * val[j] sum += rec[i][k] + rec[k][j] rec[i][j] = max(rec[i][j], sum) } } } return rec[0][n+1] }
funcmax(x, y int)int { if x > y { return x } return y }
[sol2-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
intmaxCoins(int* nums, int numsSize) { int rec[numsSize + 2][numsSize + 2]; memset(rec, 0, sizeof(rec)); int val[numsSize + 2]; val[0] = val[numsSize + 1] = 1; for (int i = 1; i <= numsSize; i++) { val[i] = nums[i - 1]; } for (int i = numsSize - 1; i >= 0; i--) { for (int j = i + 2; j <= numsSize + 1; j++) { for (int k = i + 1; k < j; k++) { int sum = val[i] * val[k] * val[j]; sum += rec[i][k] + rec[k][j]; rec[i][j] = fmax(rec[i][j], sum); } } } return rec[0][numsSize + 1]; }