We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1a343a2 commit 327fbbbCopy full SHA for 327fbbb
Greedy/881.Boats-to-Save-People/881.Boats-to-Save-People_v3.cpp
@@ -7,23 +7,21 @@ class Solution {
7
p[w]++;
8
9
int count = 0;
10
+ int x = limit;
11
int y = 1;
- for (int x = limit; x>0; x--)
12
+
13
+ while (x>0)
14
{
- for (int i = p[x]; i>0; i--)
- {
15
- count++;
16
- p[x]--;
17
-
18
- while (y+x<=limit && p[y]==0) y++;
19
- if (y+x<=limit && p[y]>0)
20
21
- p[y]--;
22
- if (x==y) i--;
23
- }
24
25
+ while (x>0 && p[x]==0)
+ x--;
+ if (x==0) break;
+ count++;
+ p[x]--;
+ while (y+x<=limit && p[y]==0) y++;
+ if (y+x<=limit && p[y]>0) p[y]--;
26
}
27
return count;
28
29
};
0 commit comments