Ctrl K

Score Spread of Parcel Groups

Hard
Parcels lie on a conveyor belt; weights[i] is the weight of parcel i. You must cut the belt into groups non-empty groups, each a contiguous stretch of the belt (the order of parcels cannot change). The score of a group is the weight of its first parcel plus the weight of its last parcel (a single-parcel group counts its weight twice). The score of a split is the sum of the scores of all its groups. Return the difference between the largest and the smallest score over all possible splits.
Example 1
weights=[2, 6, 1, 4], groups=2 → 3

Cut after parcel 0: (2 + 2) + (6 + 4) = 14; after parcel 1: (2 + 6) + (1 + 4) = 13; after parcel 2: (2 + 1) + (4 + 4) = 11. Max 14, min 11, difference 3.

Example 2
weights=[5, 9, 3], groups=3 → 0

Every parcel forms its own group, so there is only one split and the difference is 0.

Example 3
weights=[1, 8, 2, 7, 3], groups=3 → 2
Constraints
  • 1 ≤ groups ≤ len(weights) ≤ 10⁵
  • 1 ≤ weights[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsGrowing Capital Project by Project HardMost Orders by Their Deadlines HardNarrowest Range Touching Every List HardCrew Paid by Skill HardThe Slowest Sets the Squad's Pace Hard
loading Python…
Input
weights=[2, 6, 1, 4], groups=2
Expected
3

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