Largest Sum of Averages - Problem
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.
Input & Output
Example 1 — Basic Partitioning
$
Input:
nums = [9,1,2,3], k = 3
›
Output:
13.5
💡 Note:
Best partition is [9], [1,2], [3] with averages 9 + 1.5 + 3 = 13.5.
Example 2 — Single Partition
$
Input:
nums = [1,2,3,4,5,6,7], k = 4
›
Output:
20.83333
💡 Note:
With k=4 partitions allowed, we can split into groups like [1],[2,3],[4,5],[6,7] giving averages 1 + 2.5 + 4.5 + 6.5 = 14.5, or try other combinations to maximize the sum of averages.
Example 3 — No Partitioning Needed
$
Input:
nums = [4,1,7,9], k = 1
›
Output:
5.25
💡 Note:
With k=1, we must use the entire array as one partition: [4,1,7,9] with average (4+1+7+9)/4 = 21/4 = 5.25.
Constraints
- 1 ≤ nums.length ≤ 100
- 1 ≤ k ≤ nums.length
- 0 ≤ nums[i] ≤ 104
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code