We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Largest Sum of Averages

Number: 831

Difficulty: Medium

Paid? No

Companies: Google


Problem Description

Given an integer array and an integer k, partition the array into at most k non-empty adjacent subarrays. The challenge is to maximize the score defined as the sum of the averages of each of these subarrays. Every element in the array must belong to some subarray, and the answer is accepted if it is within 10^-6 of the true maximum score.


Key Insights

  • Use Dynamic Programming to break the problem into subproblems: finding the optimal partitioning up to a point with a certain number of groups.
  • Leverage a prefix sum array to efficiently calculate the sum of any subarray in constant time.
  • DP state dp[i][j] can represent the maximum score achievable using the first i numbers partitioned into j groups.
  • Base case: when only one partition is used, the score is simply the average of the first i elements.
  • Transitions: For each dp[i][j], iterate over all possible previous partition indexes to determine the best way to start the last partition.

Space and Time Complexity

Time Complexity: O(n^2 * k), due to nested loops over the array indices for each partition count. Space Complexity: O(n * k) for the DP table, plus O(n) for the prefix sum array.


Solution

The solution employs a dynamic programming (DP) approach in combination with prefix sums:

  1. Compute the prefix sum array to enable O(1) range sum queries.
  2. Initialize the DP table where dp[i][1] is set to the average of the first i elements.
  3. For each possible partition count from 2 to k, iterate over the ending index and choose the best partition point, updating dp[i][partition] as the maximum value between the current and previously computed values.
  4. The final answer is found at dp[n][k], indicating the maximum score for partitioning the entire array into at most k subarrays.

Code Solutions

# Python solution using dynamic programming and prefix sum

def largestSumOfAverages(nums, k):
    n = len(nums)
    # Build prefix sum array: prefix[i] is the sum of nums[0] to nums[i-1]
    prefix = [0] * (n + 1)
    for i in range(n):
        prefix[i + 1] = prefix[i] + nums[i]
    
    # Initialize dp table where dp[i][j] is the maximum score for first i elements divided into j partitions
    dp = [[0] * (k + 1) for _ in range(n + 1)]
    
    # Base case: using one partition, the score is the average of first i elements
    for i in range(1, n + 1):
        dp[i][1] = prefix[i] / i
    
    # Fill dp table for partition counts from 2 to k
    for partition in range(2, k + 1):
        for i in range(partition, n + 1):
            # Evaluate all potential partition points from which the current partition starts
            for j in range(partition - 1, i):
                current_avg = (prefix[i] - prefix[j]) / (i - j)
                dp[i][partition] = max(dp[i][partition], dp[j][partition - 1] + current_avg)
    
    return dp[n][k]

# Example usage:
print(largestSumOfAverages([9,1,2,3,9], 3))
← Back to All Questions