Skip to content

Commit 684e73f

Browse files
authored
Update divide-an-array-into-subarrays-with-minimum-cost-ii.cpp
1 parent 3dad954 commit 684e73f

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

C++/divide-an-array-into-subarrays-with-minimum-cost-ii.cpp

+24-22
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,19 @@ class Solution {
4848
template<typename T>
4949
void lazy_delete(T& heap, int& total, const int d) {
5050
++total;
51-
if (total > size(heap) - total) {
52-
T new_heap;
53-
while (!empty(heap)) {
54-
const auto x = heap.top(); heap.pop();
55-
if (-x.second <= d) {
56-
--total;
57-
continue;
58-
}
59-
new_heap.emplace(x);
51+
if (total <= size(heap) - total) {
52+
return;
53+
}
54+
T new_heap;
55+
while (!empty(heap)) {
56+
const auto x = heap.top(); heap.pop();
57+
if (-x.second <= d) {
58+
--total;
59+
continue;
6060
}
61-
heap = move(new_heap);
61+
new_heap.emplace(x);
6262
}
63+
heap = move(new_heap);
6364
}
6465
};
6566

@@ -118,21 +119,22 @@ class Solution2 {
118119
void lazy_delete(T& heap, auto& cnt, int& total, int x) {
119120
++cnt[x];
120121
++total;
121-
if (total > size(heap) - total) {
122-
T new_heap;
123-
while (!empty(heap)) {
124-
const auto x = heap.top(); heap.pop();
125-
if (cnt.count(x)) {
126-
if (--cnt[x] == 0) {
127-
cnt.erase(x);
128-
}
129-
continue;
122+
if (total <= size(heap) - total) {
123+
return;
124+
}
125+
T new_heap;
126+
while (!empty(heap)) {
127+
const auto x = heap.top(); heap.pop();
128+
if (cnt.count(x)) {
129+
if (--cnt[x] == 0) {
130+
cnt.erase(x);
130131
}
131-
new_heap.emplace(x);
132+
continue;
132133
}
133-
total = 0;
134-
heap = move(new_heap);
134+
new_heap.emplace(x);
135135
}
136+
total = 0;
137+
heap = move(new_heap);
136138
}
137139
};
138140

0 commit comments

Comments
 (0)