Ctrl K

Split for the Best Averages

Medium
An analyst looks at a row of daily readings values and wants to cut it into runs of consecutive days, at most parts of them. Every day must belong to exactly one run, runs are non-empty and keep the original order. The score of a split is the sum of the averages of its runs. Return the highest score you can get. The answer is a real number; anything within 10⁻⁶ of it is accepted.
Example 1
values=[6, 2, 4, 10], parts=2 → 14.0

The best cut takes off the last element: [6, 2, 4] and [10]. The averages are 4 and 10, total 14.

Example 2
values=[3, 7, 5], parts=3 → 15.0

Every element is its own piece: 3 + 7 + 5 = 15.

Example 3
values=[1, 2, 6], parts=1 → 3.0

No cuts allowed, so there is one piece with average (1 + 2 + 6) / 3 = 3.

Constraints
  • 1 ≤ len(values) ≤ 100
  • 1 ≤ values[i] ≤ 10⁴
  • 1 ≤ parts ≤ len(values)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsThe Growing Grab Limit MediumSegments With an Odd Sum MediumCalm Before and After MediumValley Floor Points MediumPass the Load to the Left Medium
loading Python…
Input
values=[6, 2, 4, 10], parts=2
Expected
14.0

Run checks the first 3 tests in your browser. Submit checks all 103 tests on the server, hidden ones included.