Practice Put Marbles in Bags
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Partition weights into exactly k nonempty consecutive groups, preserving marble order. A group from i through j costs weights[i] + weights[j], even when i = j. Return the largest total group cost minus the smallest total group cost among valid partitions.
Source: Put Marbles in Bags on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
Input
weights = [1,3,5,1], k = 2
Output
4
# Example 2
Input
weights = [1, 3], k = 2
Output
0
Constraints
1 <= k <= weights.length <= 10^5
1 <= weights[i] <= 10^9
Related LeetCode Problems
Practice more problems similar to Put Marbles in Bags.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.