Skip to content

Commit cb36597

Browse files
authored
Update special-array-with-x-elements-greater-than-or-equal-x.cpp
1 parent d590bbb commit cb36597

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

C++/special-array-with-x-elements-greater-than-or-equal-x.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,11 @@ class Solution2 {
5050
count[i] += count[i - 1];
5151
}
5252
for (int i = size(*nums) - 1; i >= 0; --i) { // inplace but unstable sort
53-
if ((*nums)[i] < 0) { // processed
54-
continue;
55-
}
56-
while (i != count[(*nums)[i]] - 1) {
53+
while ((*nums)[i] >= 0) {
5754
--count[(*nums)[i]];
58-
tie((*nums)[count[(*nums)[i]]], (*nums)[i]) = pair(~(*nums)[i], (*nums)[count[(*nums)[i]]]);
55+
const int j = count[(*nums)[i]];
56+
tie((*nums)[i], (*nums)[j]) = pair((*nums)[j], ~(*nums)[i]);
5957
}
60-
--count[(*nums)[i]];
61-
(*nums)[i] = ~(*nums)[i];
6258
}
6359
for (auto& num : *nums) {
6460
num = ~num; // restore values

0 commit comments

Comments
 (0)