Skip to content

Commit 5b522d9

Browse files
committed
fix typo
1 parent de5218e commit 5b522d9

1 file changed

Lines changed: 79 additions & 79 deletions

File tree

articles/put-marbles-in-bags.md

Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -651,24 +651,24 @@ class Solution:
651651
if k == 1:
652652
return 0
653653

654-
max_heap = []
655654
min_heap = []
655+
max_heap = []
656656

657657
for i in range(len(weights) - 1):
658658
split = weights[i] + weights[i + 1]
659659

660-
if len(max_heap) < k - 1:
661-
heapq.heappush(max_heap, split)
660+
if len(min_heap) < k - 1:
661+
heapq.heappush(min_heap, split)
662662
else:
663-
heapq.heappushpop(max_heap, split)
663+
heapq.heappushpop(min_heap, split)
664664

665-
if len(min_heap) < k - 1:
666-
heapq.heappush(min_heap, -split)
665+
if len(max_heap) < k - 1:
666+
heapq.heappush(max_heap, -split)
667667
else:
668-
heapq.heappushpop(min_heap, -split)
668+
heapq.heappushpop(max_heap, -split)
669669

670-
max_score = sum(max_heap)
671-
min_score = -sum(min_heap)
670+
max_score = sum(min_heap)
671+
min_score = -sum(max_heap)
672672
return max_score - min_score
673673
```
674674

@@ -677,28 +677,28 @@ public class Solution {
677677
public long putMarbles(int[] weights, int k) {
678678
if (k == 1) return 0L;
679679

680-
PriorityQueue<Integer> maxHeap = new PriorityQueue<>();
681-
PriorityQueue<Integer> minHeap = new PriorityQueue<>(Collections.reverseOrder());
680+
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
681+
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
682682

683683
for (int i = 0; i < weights.length - 1; i++) {
684684
int split = weights[i] + weights[i + 1];
685685

686-
if (maxHeap.size() < k - 1) maxHeap.offer(split);
687-
else if (split > maxHeap.peek()) {
688-
maxHeap.poll();
689-
maxHeap.offer(split);
690-
}
691-
692686
if (minHeap.size() < k - 1) minHeap.offer(split);
693-
else if (split < minHeap.peek()) {
687+
else if (split > minHeap.peek()) {
694688
minHeap.poll();
695689
minHeap.offer(split);
696690
}
691+
692+
if (maxHeap.size() < k - 1) maxHeap.offer(split);
693+
else if (split < maxHeap.peek()) {
694+
maxHeap.poll();
695+
maxHeap.offer(split);
696+
}
697697
}
698698

699699
long maxScore = 0, minScore = 0;
700-
for (int val : maxHeap) maxScore += val;
701-
for (int val : minHeap) minScore += val;
700+
for (int val : minHeap) maxScore += val;
701+
for (int val : maxHeap) minScore += val;
702702

703703
return maxScore - minScore;
704704
}
@@ -711,34 +711,34 @@ public:
711711
long long putMarbles(vector<int>& weights, int k) {
712712
if (k == 1) return 0LL;
713713

714-
priority_queue<int, vector<int>, greater<int>> maxHeap;
715-
priority_queue<int> minHeap;
714+
priority_queue<int, vector<int>, greater<int>> minHeap;
715+
priority_queue<int> maxHeap;
716716

717717
for (int i = 0; i < weights.size() - 1; ++i) {
718718
int split = weights[i] + weights[i + 1];
719719

720-
if ((int)maxHeap.size() < k - 1) maxHeap.push(split);
721-
else if (split > maxHeap.top()) {
722-
maxHeap.pop();
723-
maxHeap.push(split);
724-
}
725-
726720
if ((int)minHeap.size() < k - 1) minHeap.push(split);
727-
else if (split < minHeap.top()) {
721+
else if (split > minHeap.top()) {
728722
minHeap.pop();
729723
minHeap.push(split);
730724
}
725+
726+
if ((int)maxHeap.size() < k - 1) maxHeap.push(split);
727+
else if (split < maxHeap.top()) {
728+
maxHeap.pop();
729+
maxHeap.push(split);
730+
}
731731
}
732732

733733
long long maxScore = 0, minScore = 0;
734-
while (!maxHeap.empty()) {
735-
maxScore += maxHeap.top();
736-
maxHeap.pop();
737-
}
738734
while (!minHeap.empty()) {
739-
minScore += minHeap.top();
735+
maxScore += minHeap.top();
740736
minHeap.pop();
741737
}
738+
while (!maxHeap.empty()) {
739+
minScore += maxHeap.top();
740+
maxHeap.pop();
741+
}
742742

743743
return maxScore - minScore;
744744
}
@@ -783,24 +783,24 @@ public class Solution {
783783
public long PutMarbles(int[] weights, int k) {
784784
if (k == 1) return 0L;
785785

786-
var maxHeap = new PriorityQueue<int, int>();
787-
var minHeap = new PriorityQueue<int, int>(
786+
var minHeap = new PriorityQueue<int, int>();
787+
var maxHeap = new PriorityQueue<int, int>(
788788
Comparer<int>.Create((a, b) => b.CompareTo(a))
789789
);
790790

791791
for (int i = 0; i < weights.Length - 1; i++) {
792792
int split = weights[i] + weights[i + 1];
793793

794-
maxHeap.Enqueue(split, split);
795-
if (maxHeap.Count > k - 1) maxHeap.Dequeue();
796-
797794
minHeap.Enqueue(split, split);
798795
if (minHeap.Count > k - 1) minHeap.Dequeue();
796+
797+
maxHeap.Enqueue(split, split);
798+
if (maxHeap.Count > k - 1) maxHeap.Dequeue();
799799
}
800800

801801
long maxScore = 0, minScore = 0;
802-
while (maxHeap.Count > 0) maxScore += maxHeap.Dequeue();
803-
while (minHeap.Count > 0) minScore += minHeap.Dequeue();
802+
while (minHeap.Count > 0) maxScore += minHeap.Dequeue();
803+
while (maxHeap.Count > 0) minScore += maxHeap.Dequeue();
804804

805805
return maxScore - minScore;
806806
}
@@ -813,31 +813,31 @@ func putMarbles(weights []int, k int) int64 {
813813
return 0
814814
}
815815

816-
maxHeap := &MinHeap{}
817-
minHeap := &MaxHeap{}
818-
heap.Init(maxHeap)
816+
minHeap := &MinHeap{}
817+
maxHeap := &MaxHeap{}
819818
heap.Init(minHeap)
819+
heap.Init(maxHeap)
820820

821821
for i := 0; i < len(weights)-1; i++ {
822822
split := weights[i] + weights[i+1]
823823

824-
heap.Push(maxHeap, split)
825-
if maxHeap.Len() > k-1 {
826-
heap.Pop(maxHeap)
827-
}
828-
829824
heap.Push(minHeap, split)
830825
if minHeap.Len() > k-1 {
831826
heap.Pop(minHeap)
832827
}
828+
829+
heap.Push(maxHeap, split)
830+
if maxHeap.Len() > k-1 {
831+
heap.Pop(maxHeap)
832+
}
833833
}
834834

835835
var maxScore, minScore int64
836-
for maxHeap.Len() > 0 {
837-
maxScore += int64(heap.Pop(maxHeap).(int))
838-
}
839836
for minHeap.Len() > 0 {
840-
minScore += int64(heap.Pop(minHeap).(int))
837+
maxScore += int64(heap.Pop(minHeap).(int))
838+
}
839+
for maxHeap.Len() > 0 {
840+
minScore += int64(heap.Pop(maxHeap).(int))
841841
}
842842

843843
return maxScore - minScore
@@ -875,23 +875,23 @@ class Solution {
875875
fun putMarbles(weights: IntArray, k: Int): Long {
876876
if (k == 1) return 0L
877877

878-
val maxHeap = PriorityQueue<Int>()
879-
val minHeap = PriorityQueue<Int>(reverseOrder())
878+
val minHeap = PriorityQueue<Int>()
879+
val maxHeap = PriorityQueue<Int>(reverseOrder())
880880

881881
for (i in 0 until weights.size - 1) {
882882
val split = weights[i] + weights[i + 1]
883883

884-
maxHeap.offer(split)
885-
if (maxHeap.size > k - 1) maxHeap.poll()
886-
887884
minHeap.offer(split)
888885
if (minHeap.size > k - 1) minHeap.poll()
886+
887+
maxHeap.offer(split)
888+
if (maxHeap.size > k - 1) maxHeap.poll()
889889
}
890890

891891
var maxScore = 0L
892892
var minScore = 0L
893-
while (maxHeap.isNotEmpty()) maxScore += maxHeap.poll()
894-
while (minHeap.isNotEmpty()) minScore += minHeap.poll()
893+
while (minHeap.isNotEmpty()) maxScore += minHeap.poll()
894+
while (maxHeap.isNotEmpty()) minScore += maxHeap.poll()
895895

896896
return maxScore - minScore
897897
}
@@ -903,29 +903,29 @@ class Solution {
903903
func putMarbles(_ weights: [Int], _ k: Int) -> Int64 {
904904
if k == 1 { return 0 }
905905

906-
var maxHeap = [Int]()
907906
var minHeap = [Int]()
907+
var maxHeap = [Int]()
908908

909909
for i in 0..<(weights.count - 1) {
910910
let split = weights[i] + weights[i + 1]
911911

912-
maxHeap.append(split)
913-
maxHeap.sort()
914-
if maxHeap.count > k - 1 {
915-
maxHeap.removeFirst()
916-
}
917-
918912
minHeap.append(split)
919-
minHeap.sort(by: >)
913+
minHeap.sort()
920914
if minHeap.count > k - 1 {
921915
minHeap.removeFirst()
922916
}
917+
918+
maxHeap.append(split)
919+
maxHeap.sort(by: >)
920+
if maxHeap.count > k - 1 {
921+
maxHeap.removeFirst()
922+
}
923923
}
924924

925925
var maxScore: Int64 = 0
926926
var minScore: Int64 = 0
927-
for val in maxHeap { maxScore += Int64(val) }
928-
for val in minHeap { minScore += Int64(val) }
927+
for val in minHeap { maxScore += Int64(val) }
928+
for val in maxHeap { minScore += Int64(val) }
929929

930930
return maxScore - minScore
931931
}
@@ -940,27 +940,27 @@ impl Solution {
940940
}
941941
let k = k as usize;
942942

943-
let mut max_heap = BinaryHeap::new();
944943
let mut min_heap: BinaryHeap<Reverse<i32>> = BinaryHeap::new();
944+
let mut max_heap: BinaryHeap<i32> = BinaryHeap::new();
945945

946946
for i in 0..weights.len() - 1 {
947947
let split = weights[i] + weights[i + 1];
948948

949949
// min-heap of size k-1 to keep k-1 largest
950-
max_heap.push(Reverse(split));
951-
if max_heap.len() > k - 1 {
952-
max_heap.pop();
953-
}
954-
955-
// max-heap of size k-1 to keep k-1 smallest
956950
min_heap.push(Reverse(split));
957951
if min_heap.len() > k - 1 {
958952
min_heap.pop();
959953
}
954+
955+
// max-heap of size k-1 to keep k-1 smallest
956+
max_heap.push(split);
957+
if max_heap.len() > k - 1 {
958+
max_heap.pop();
959+
}
960960
}
961961

962-
let max_score: i64 = max_heap.iter().map(|&Reverse(x)| x as i64).sum();
963-
let min_score: i64 = min_heap.iter().map(|&Reverse(x)| x as i64).sum();
962+
let max_score: i64 = min_heap.iter().map(|&Reverse(x)| x as i64).sum();
963+
let min_score: i64 = max_heap.iter().map(|&x| x as i64).sum();
964964

965965
max_score - min_score
966966
}

0 commit comments

Comments
 (0)