Put Marbles in Bags - Complete Solution Guide
Put Marbles in Bags is LeetCode problem 2551, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You have k bags. You are given a 0-indexed integer array weights where weights[i] is the weight of the i th marble. You are also given the integer k. Divide the marbles into the k bags according to the following rules: No bag is empty. If the i th marble and j th marble are in a bag, then all marbles with an index between the i th and j th indices should also be in that same bag. If a bag consists of all the marbles with an index from i to j inclusively, then the cost of the bag is weights[i] +
Detailed Explanation
The problem asks us to divide an array of marble weights into `k` bags. The cost of each bag is the sum of the weights of the first and last marble in the bag. The goal is to find the difference between the maximum and minimum possible total cost across all valid divisions. A valid division must have no empty bags, and if marbles `i` and `j` are in the same bag, all marbles between them must also be in the same bag. The core challenge is to efficiently find the maximum and minimum scores achievable.
Solution Approach
The solution computes the sums of adjacent weights in the `weights` array and stores these in a new array called `pair_sums`. This `pair_sums` array is then sorted. To maximize the total score, we select the `k-1` largest elements from the sorted `pair_sums` and sum them. To minimize the total score, we select the `k-1` smallest elements from the sorted `pair_sums` and sum them. Finally, we return the difference between these two sums.
Step-by-Step Algorithm
- Step 1: Handle the base cases where `k` is 1 or `k` is equal to the length of `weights`. In these cases, return 0.
- Step 2: Create a `pair_sums` array to store the sums of adjacent elements in the `weights` array. For each `i` from 0 to `n-2`, calculate `weights[i] + weights[i+1]` and store it in `pair_sums[i]`.
- Step 3: Sort the `pair_sums` array in ascending order.
- Step 4: Calculate the minimum score sum by summing the first `k-1` elements of the sorted `pair_sums` array.
- Step 5: Calculate the maximum score sum by summing the last `k-1` elements of the sorted `pair_sums` array.
- Step 6: Return the difference between the maximum score sum and the minimum score sum.
Key Insights
- Insight 1: The sum of the first and last elements of the original `weights` array is always included in the total score, regardless of how the marbles are divided into bags. Therefore, optimizing the score involves maximizing or minimizing the sums of weights at the 'cut points' where bags are split.
- Insight 2: The problem can be reframed to finding the `k-1` largest and `k-1` smallest sums of adjacent weights, and calculating the difference of their sums, since each cut contributes the sum of the weights immediately before and after that cut.
- Insight 3: The trivial cases of `k = 1` or `k = n` result in a difference of 0, because there's only one possible distribution.
Complexity Analysis
Time Complexity: O(n log n)
Space Complexity: O(n)
Topics
This problem involves: Array, Greedy, Sorting, Heap (Priority Queue).
Companies
Asked at: DE Shaw, Flipkart.