You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|[Leetcode-1888](https://leetcode.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/)| Minimum Number Of Flips To Make The Binary String Alternating |[c++](./leetcode/1888.minimum-number-of-flips-to-make-the-binary-string-alternating.cpp), [python3](./leetcode/1888.minimum-number-of-flips-to-make-the-binary-string-alternating.py)| Greedy | O\(N\)| O\(1\)| - |
|[Leetcode-2551](https://leetcode.com/problems/put-marbles-in-bags/)| Put Marbles In Bags |[c++](./leetcode/2551.put-marbles-in-bags.cpp), [python3](./leetcode/2551.put-marbles-in-bags.py)| Greedy | O\(N\)| O\(N\)| - |
299
300
|[Leetcode-3282](https://leetcode.com/problems/reach-end-of-array-with-max-score/)| Reach End Of Array With Max Score |[c++](./leetcode/3282.reach-end-of-array-with-max-score.cpp), [python3](./leetcode/3282.reach-end-of-array-with-max-score.py)| Greedy | O\(N\)| O\(1\)| - |
300
301
|[Leetcode-3439](https://leetcode.com/problems/reschedule-meetings-for-maximum-free-time-i/)| Reschedule Meetings For Maximum Free Time I |[c++](./leetcode/3439.reschedule-meetings-for-maximum-free-time-i.cpp), [python3](./leetcode/3439.reschedule-meetings-for-maximum-free-time-i.py)| Greedy | O\(N\)| O\(N\)| - |
301
302
|[Leetcode-632](https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/)| Smallest Range Covering Elements From K Lists |[c++](./leetcode/632.smallest-range-covering-elements-from-k-lists.cpp), [python3](./leetcode/632.smallest-range-covering-elements-from-k-lists.py)| Greedy | O\(NlogN\)| O\(N\)| - |
|[Leetcode-1514](https://leetcode.com/problems/path-with-maximum-probability/)| Path With Maximum Probability |[c++](./leetcode/1514.path-with-maximum-probability.cpp), [python3](./leetcode/1514.path-with-maximum-probability.py)| Heap | O\(VlogE\)| O\(V\+E\)| - |
|[Leetcode-3306](https://leetcode.com/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/)| Count Of Substrings Containing Every Vowel And K Consonants II |[c++](./leetcode/3306.count-of-substrings-containing-every-vowel-and-k-consonants-ii.cpp), [python3](./leetcode/3306.count-of-substrings-containing-every-vowel-and-k-consonants-ii.py)| Sliding Window | O\(N\)| O\(N\)| - |
820
+
|[Leetcode-2962](https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times/)| Count Subarrays Where Max Element Appears At Least K Times |[c++](./leetcode/2962.count-subarrays-where-max-element-appears-at-least-k-times.cpp), [python3](./leetcode/2962.count-subarrays-where-max-element-appears-at-least-k-times.py)| Sliding Window | O\(N\)| O\(1\)| - |
|[Leetcode-1976](https://leetcode.com/problems/number-of-ways-to-arrive-at-destination/)| Number Of Ways To Arrive At Destination |[c++](./leetcode/1976.number-of-ways-to-arrive-at-destination.cpp), [python3](./leetcode/1976.number-of-ways-to-arrive-at-destination.py)| Sorting | O\(\(E \+ V\)logV\)| O\(E \+ V\)| - |
944
+
|[Leetcode-2551](https://leetcode.com/problems/put-marbles-in-bags/)| Put Marbles In Bags |[c++](./leetcode/2551.put-marbles-in-bags.cpp), [python3](./leetcode/2551.put-marbles-in-bags.py)| Sorting | O\(N\)| O\(N\)| - |
// 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.
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
+
//
15
+
// The score after distributing the marbles is the sum of the costs of all the k bags.
16
+
// Return the difference between the maximum and minimum scores among marble distributions.
17
+
//
18
+
// Example 1:
19
+
//
20
+
// Input: weights = [1,3,5,1], k = 2
21
+
// Output: 4
22
+
// Explanation:
23
+
// The distribution [1],[3,5,1] results in the minimal score of (1+1) + (3+1) = 6.
24
+
// The distribution [1,3],[5,1], results in the maximal score of (1+3) + (5+1) = 10.
25
+
// Thus, we return their difference 10 - 6 = 4.
26
+
//
27
+
// Example 2:
28
+
//
29
+
// Input: weights = [1, 3], k = 2
30
+
// Output: 0
31
+
// Explanation: The only distribution possible is [1],[3].
32
+
// Since both the maximal and minimal score are the same, we return 0.
33
+
//
34
+
//
35
+
// Constraints:
36
+
//
37
+
// 1 <= k <= weights.length <= 105
38
+
// 1 <= weights[i] <= 109
39
+
//
40
+
//
41
+
42
+
classSolution {
43
+
public:
44
+
longlongputMarbles(vector<int>& weights, int k) {
45
+
int n = weights.size();
46
+
if (k == n) {
47
+
return0;
48
+
}
49
+
50
+
k = k - 1;
51
+
vector<int> splits(n - 1, 0);
52
+
for (int i = 0; i < n - 1; i++) {
53
+
splits[i] = weights[i] + weights[i + 1];
54
+
55
+
}
56
+
sort(splits.begin(), splits.end());
57
+
58
+
longlong diff = 0;
59
+
for (int i = 0; i < k; i++) {
60
+
diff += splits[splits.size() - i - 1] - splits[i];
0 commit comments