More elegant solution


#1

I found my solution more elegant. I would be interested in your comments. The code is in java:

public class Solution {

public int maxcoin(ArrayList<Integer> a) {
    int n = a.size();
    long[] ds = new long[n + 1];
    long[][] dp = new long[n][n];
    
    for (int i = 0; i < n; i++) {
        ds[i + 1] = ds[i] + a.get(i);
        dp[i][i] = a.get(i);
    }
    
    for (int k = 1; k < n; k++) {
        for (int i = 0; i < n - k; i++) {
            int j = i + k;
            dp[i][j] = sum(ds, i, j) - Math.min(dp[i][j - 1], dp[i + 1][j]);
        }
    }
    return (int) dp[0][n - 1];
}

long sum(long[] ds, int st, int end) {
  return ds[end + 1] - ds[st];  
}
}