Formatted question description: https://leetcode.ca/all/813.html

813. Largest Sum of Averages (Medium)

We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?

Note that our partition must use every number in A, and that scores are not necessarily integers.

Example:
Input: 
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation: 
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.

 

Note:

  • 1 <= A.length <= 100.
  • 1 <= A[i] <= 10000.
  • 1 <= K <= A.length.
  • Answers within 10^-6 of the correct answer will be accepted as correct.

Related Topics:
Dynamic Programming

Solution 1. DP

Let dp[k][i] be the maximum score for the subproblem with k groups and A[0..i] subarray.

Let avg[i][j] be the average of numbers in subarray A[i..j].

dp[1][i] = avg[0][i]
dp[k][i] = max(dp[k][i], dp[k-1][j] + avg[j][i] | k-1 <= j <= i)    where k >= 2
// OJ: https://leetcode.com/problems/largest-sum-of-averages/

// Time: O(N^2 * K)
// Space: O(NK + N^2)
class Solution {
public:
    double largestSumOfAverages(vector<int>& A, int K) {
        int N = A.size();
        vector<vector<double>> avg(N + 1, vector<double>(N + 1));
        for (int i = 0; i < N; ++i) {
            int sum = 0;
            for (int j = i; j < N; ++j) {
                sum += A[j];
                avg[i][j] = (double)sum / (j - i + 1);
            }
        }
        vector<vector<double>> dp(K + 1, vector<double>(N));
        for (int i = 0; i < N; ++i) dp[1][i] = avg[0][i];
        for (int k = 2; k <= K; ++k) {
            for (int i = 0; i < N; ++i) {
                for (int j = k - 1; j <= i; ++j) {
                    dp[k][i] = max(dp[k][i], dp[k - 1][j - 1] + avg[j][i]);
                }
            }
        }
        return dp[K][N - 1];
    }
};

Solution 2. DP

Count avg on the fly.

// OJ: https://leetcode.com/problems/largest-sum-of-averages/

// Time: O(N^2 * K)
// Space: O(NK)
class Solution {
public:
    double largestSumOfAverages(vector<int>& A, int K) {
        int N = A.size();
        vector<vector<double>> dp(K + 1, vector<double>(N));
        double sum = 0;
        for (int i = 0; i < N; ++i) {
            sum += A[i];
            dp[1][i] = sum / (i + 1);
        }
        for (int k = 2; k <= K; ++k) {
            for (int i = k - 1; i < N; ++i) {
                double sum = 0;
                for (int j = i; j >= k - 1; --j) {
                    sum += A[j];
                    dp[k][i] = max(dp[k][i], dp[k - 1][j - 1] + sum / (i - j + 1));
                }
            }
        }
        return dp[K][N - 1];
    }
};

Solution 3. DP + Space Optimization

Since dp[k][i] is only dependent on dp[k-1][j-1] and j <= i, we can reduce the dp array to 1D array.

// OJ: https://leetcode.com/problems/largest-sum-of-averages/

// Time: O(N^2 * K)
// Space: O(N)
class Solution {
public:
    double largestSumOfAverages(vector<int>& A, int K) {
        int N = A.size();
        vector<double> dp(N);
        double sum = 0;
        for (int i = 0; i < N; ++i) {
            sum += A[i];
            dp[i] = sum / (i + 1);
        }
        for (int k = 2; k <= K; ++k) {
            for (int i = N - 1; i >= k - 1; --i) {
                double sum = 0;
                dp[i] = 0;
                for (int j = i; j >= k - 1; --j) {
                    sum += A[j];
                    dp[i] = max(dp[i], dp[j - 1] + sum / (i - j + 1));
                }
            }
        }
        return dp[N - 1];
    }
};

Java

class Solution {
    public double largestSumOfAverages(int[] A, int K) {
        int length = A.length;
        double[] prefixSums = new double[length + 1];
        for (int i = 1; i <= length; i++)
            prefixSums[i] = prefixSums[i - 1] + A[i - 1];
        double[][] dp = new double[length + 1][K + 1];
        for (int i = 1; i <= length; i++) {
            dp[i][1] = prefixSums[i] / i;
            int maxGroups = Math.min(i, K);
            for (int k = 2; k <= maxGroups; k++) {
                for (int j = 1; j < i; j++)
                    dp[i][k] = Math.max(dp[i][k], dp[j][k - 1] + (prefixSums[i] - prefixSums[j]) / (i - j));
            }
        }
        return dp[length][K];
    }
}

All Problems

All Solutions