Skip to content

Commit 03d2146

Browse files
committed
leetcode
1 parent fd4c14f commit 03d2146

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package main
2+
3+
import "container/heap"
4+
5+
type MinHeap []int
6+
7+
func (h MinHeap) Len() int { return len(h) }
8+
func (h MinHeap) Less(i, j int) bool { return h[i] < h[j] }
9+
func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
10+
11+
func (h *MinHeap) Push(x interface{}) {
12+
*h = append(*h, x.(int))
13+
}
14+
15+
func (h *MinHeap) Pop() interface{} {
16+
old := *h
17+
n := len(old)
18+
x := old[n-1]
19+
*h = old[:n-1]
20+
return x
21+
}
22+
23+
type MaxHeap []int
24+
25+
func (h MaxHeap) Len() int { return len(h) }
26+
func (h MaxHeap) Less(i, j int) bool { return h[i] > h[j] }
27+
func (h MaxHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
28+
29+
func (h *MaxHeap) Push(x interface{}) {
30+
*h = append(*h, x.(int))
31+
}
32+
33+
func (h *MaxHeap) Pop() interface{} {
34+
old := *h
35+
n := len(old)
36+
x := old[n-1]
37+
*h = old[:n-1]
38+
return x
39+
}
40+
41+
func putMarbles(weights []int, k int) int64 {
42+
maxQ := &MaxHeap{}
43+
minQ := &MinHeap{}
44+
heap.Init(maxQ)
45+
heap.Init(minQ)
46+
47+
n := len(weights)
48+
for i := 0; i < n-1; i++ {
49+
heap.Push(maxQ, weights[i]+weights[i+1])
50+
if maxQ.Len() == k {
51+
heap.Pop(maxQ)
52+
}
53+
54+
heap.Push(minQ, weights[i]+weights[i+1])
55+
if minQ.Len() == k {
56+
heap.Pop(minQ)
57+
}
58+
}
59+
60+
var diff int64
61+
for len(*minQ) > 0 {
62+
diff += int64(heap.Pop(minQ).(int)) - int64(heap.Pop(maxQ).(int))
63+
}
64+
65+
return diff
66+
}

PutMarblesInBags/put_marbles_in_bags.md

Whitespace-only changes.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
244244
- [**137.** Single Number II](SingleNumberII/single_number_ii.dart)
245245
- [**1493.** Longest Subarray of 1's After Deleting One Element](LongestSubarrayOf1SAfterDeletingOneElement/longest_subarray_of_1s_after_deleting_one_element.dart)
246246
- [**2024.** Maximize the Confusion of an Exam](MaximizeTheConfusionOfAnExam/maximize_the_confusion_of_an_exam.dart)
247+
- [**2551.** Put Marbles in Bags](PutMarblesInBags)
247248

248249
## Reach me via
249250

0 commit comments

Comments
 (0)