813. Largest Sum of Averages LeetCode Solution

In this guide, you will get 813. Largest Sum of Averages LeetCode Solution with the best time and space complexity. The solution to Largest Sum of Averages problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Largest Sum of Averages solution in C++
  4. Largest Sum of Averages solution in Java
  5. Largest Sum of Averages solution in Python
  6. Additional Resources
813. Largest Sum of Averages LeetCode Solution image

Problem Statement of Largest Sum of Averages

You are given an integer array nums and an integer k. You can partition the array into at most k non-empty adjacent subarrays. The score of a partition is the sum of the averages of each subarray.
Note that the partition must use every integer in nums, and that the score is not necessarily an integer.
Return the maximum score you can achieve of all the possible partitions. Answers within 10-6 of the actual answer will be accepted.

Example 1:

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

Example 2:

Input: nums = [1,2,3,4,5,6,7], k = 4
Output: 20.50000

Constraints:

1 <= nums.length <= 100
1 <= nums[i] <= 104
1 <= k <= nums.length

See also  1204. Last Person to Fit in the Bus LeetCode Solution

Complexity Analysis

  • Time Complexity: O(Kn^2)
  • Space Complexity: O(Kn)

813. Largest Sum of Averages LeetCode Solution in C++

class Solution {
 public:
  double largestSumOfAverages(vector<int>& nums, int k) {
    const int n = nums.size();
    vector<vector<double>> mem(n + 1, vector<double>(k + 1));
    vector<double> prefix(n + 1);
    partial_sum(nums.begin(), nums.end(), prefix.begin() + 1);
    return largestSumOfAverages(nums, n, k, prefix, mem);
  }

 private:
  // Returns the maximum score to partition the first i numbers into k groups.
  double largestSumOfAverages(const vector<int>& nums, int i, int k,
                              const vector<double>& prefix,
                              vector<vector<double>>& mem) {
    if (k == 1)
      return prefix[i] / i;
    if (mem[i][k] > 0)
      return mem[i][k];

    // Try all the possible partitions.
    for (int j = k - 1; j < i; ++j)
      mem[i][k] =
          max(mem[i][k], largestSumOfAverages(nums, j, k - 1, prefix, mem) +
                             (prefix[i] - prefix[j]) / (i - j));

    return mem[i][k];
  }
};
/* code provided by PROGIEZ */

813. Largest Sum of Averages LeetCode Solution in Java

class Solution {
  public double largestSumOfAverages(int[] nums, int k) {
    final int n = nums.length;
    double[][] mem = new double[n + 1][k + 1];
    double[] prefix = new double[n + 1];

    for (int i = 0; i < n; i++)
      prefix[i + 1] = prefix[i] + nums[i];

    return largestSumOfAverages(nums, n, k, prefix, mem);
  }

  // Returns the maximum score to partition the first i numbers into k groups.
  private double largestSumOfAverages(int[] nums, int i, int k, double[] prefix, double[][] mem) {
    if (k == 1)
      return prefix[i] / i;
    if (mem[i][k] > 0)
      return mem[i][k];

    // Try all the possible partitions.
    for (int j = k - 1; j < i; ++j)
      mem[i][k] = Math.max(mem[i][k], largestSumOfAverages(nums, j, k - 1, prefix, mem) +
                                          (prefix[i] - prefix[j]) / (i - j));

    return mem[i][k];
  }
}
// code provided by PROGIEZ

813. Largest Sum of Averages LeetCode Solution in Python

class Solution {
 public:
  double largestSumOfAverages(vector<int>& nums, int K) {
    const int n = nums.size();
    // dp[i][k] := the maximum score to partition the first i nums into k groups
    vector<vector<double>> dp(n + 1, vector<double>(K + 1));
    vector<double> prefix(n + 1);

    partial_sum(nums.begin(), nums.end(), prefix.begin() + 1);

    for (int i = 1; i <= n; ++i)
      dp[i][1] = prefix[i] / i;

    for (int k = 2; k <= K; ++k)
      for (int i = k; i <= n; ++i)
        for (int j = k - 1; j < i; ++j) {
          const double average = (prefix[i] - prefix[j]) / (i - j);
          dp[i][k] = max(dp[i][k], dp[j][k - 1] + average);
        }

    return dp[n][K];
  }
};
# code by PROGIEZ

Additional Resources

See also  803. Bricks Falling When Hit LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.