|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +-* 2551. Put Marbles in Bags *- |
| 5 | +
|
| 6 | +
|
| 7 | +You have k bags. You are given a 0-indexed integer array weights where weights[i] is the weight of the ith marble. You are also given the integer k. |
| 8 | +
|
| 9 | +Divide the marbles into the k bags according to the following rules: |
| 10 | +
|
| 11 | +No bag is empty. |
| 12 | +If the ith marble and jth marble are in a bag, then all marbles with an index between the ith and jth indices should also be in that same bag. |
| 13 | +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] + weights[j]. |
| 14 | +The score after distributing the marbles is the sum of the costs of all the k bags. |
| 15 | +
|
| 16 | +Return the difference between the maximum and minimum scores among marble distributions. |
| 17 | +
|
| 18 | + |
| 19 | +
|
| 20 | +Example 1: |
| 21 | +
|
| 22 | +Input: weights = [1,3,5,1], k = 2 |
| 23 | +Output: 4 |
| 24 | +Explanation: |
| 25 | +The distribution [1],[3,5,1] results in the minimal score of (1+1) + (3+1) = 6. |
| 26 | +The distribution [1,3],[5,1], results in the maximal score of (1+3) + (5+1) = 10. |
| 27 | +Thus, we return their difference 10 - 6 = 4. |
| 28 | +Example 2: |
| 29 | +
|
| 30 | +Input: weights = [1, 3], k = 2 |
| 31 | +Output: 0 |
| 32 | +Explanation: The only distribution possible is [1],[3]. |
| 33 | +Since both the maximal and minimal score are the same, we return 0. |
| 34 | + |
| 35 | +
|
| 36 | +Constraints: |
| 37 | +
|
| 38 | +1 <= k <= weights.length <= 105 |
| 39 | +1 <= weights[i] <= 109 |
| 40 | +
|
| 41 | +
|
| 42 | +*/ |
| 43 | + |
| 44 | +class A { |
| 45 | + int putMarbles(List<int> weights, int k) { |
| 46 | + final List<int> sums = List.empty(); |
| 47 | + k--; |
| 48 | + |
| 49 | + for (int i = 0; i < weights.length - 1; i++) { |
| 50 | + sums.add(weights[i] + weights[i + 1]); |
| 51 | + } |
| 52 | + |
| 53 | + sums.sort(); |
| 54 | + |
| 55 | + int ans1 = 0; |
| 56 | + int ans2 = 0; |
| 57 | + |
| 58 | + for (int i = 0; i < sums.length - k; i++) { |
| 59 | + ans1 += sums[i]; |
| 60 | + } |
| 61 | + |
| 62 | + for (int i = k; i < sums.length; i++) { |
| 63 | + ans2 += sums[i]; |
| 64 | + } |
| 65 | + |
| 66 | + return ans2 - ans1; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +class Solution { |
| 71 | + int putMarbles(List<int> weights, int k) { |
| 72 | + List<int> pqMax = []; |
| 73 | + List<int> pqMin = []; |
| 74 | + |
| 75 | + for (int i = 1; i <= weights.length - 1; i++) { |
| 76 | + pqMax.add(i); |
| 77 | + pqMax.sort((a, b) => |
| 78 | + (weights[a] + weights[a - 1]).compareTo(weights[b] + weights[b - 1])); |
| 79 | + if (pqMax.length > k - 1) pqMax.removeAt(0); |
| 80 | + |
| 81 | + pqMin.add(i); |
| 82 | + pqMin.sort((a, b) => |
| 83 | + (weights[b] + weights[b - 1]).compareTo(weights[a] + weights[a - 1])); |
| 84 | + if (pqMin.length > k - 1) pqMin.removeAt(0); |
| 85 | + } |
| 86 | + |
| 87 | + int sumMax = pqMax |
| 88 | + .map((index) => weights[index] + weights[index - 1]) |
| 89 | + .fold(0, (sum, element) => sum + element); |
| 90 | + |
| 91 | + int sumMin = pqMin |
| 92 | + .map((index) => weights[index] + weights[index - 1]) |
| 93 | + .fold(0, (sum, element) => sum + element); |
| 94 | + |
| 95 | + return (sumMax - sumMin).abs(); |
| 96 | + } |
| 97 | +} |
0 commit comments