Skip to content

Commit 0c02eae

Browse files
authored
Update maximum-and-minimum-sums-of-at-most-size-k-subarrays.cpp
1 parent ced9f8a commit 0c02eae

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

C++/maximum-and-minimum-sums-of-at-most-size-k-subarrays.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ class Solution2 {
5151
dq.emplace_back(right, left);
5252
total += static_cast<int64_t>(right - left + 1) * nums[right];
5353
result += total;
54-
if (dq[0].first == right - (k - 1)) {
54+
if (right - (k - 1) >= 0) {
5555
total -= nums[dq[0].first];
56-
dq.pop_front();
57-
} else if (dq[0].second == right - (k - 1)) {
58-
total -= nums[dq[0].first];
59-
++dq[0].second;
56+
if (dq[0].first == right - (k - 1)) {
57+
dq.pop_front();
58+
} else {
59+
assert(dq[0].second == right - (k - 1));
60+
++dq[0].second;
61+
}
6062
}
6163
}
6264
return result;

0 commit comments

Comments
 (0)