Skip to content

Commit 327fbbb

Browse files
authored
Update 881.Boats-to-Save-People_v3.cpp
1 parent 1a343a2 commit 327fbbb

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

Greedy/881.Boats-to-Save-People/881.Boats-to-Save-People_v3.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,21 @@ class Solution {
77
p[w]++;
88

99
int count = 0;
10+
int x = limit;
1011
int y = 1;
11-
for (int x = limit; x>0; x--)
12+
13+
while (x>0)
1214
{
13-
for (int i = p[x]; i>0; i--)
14-
{
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-
}
15+
while (x>0 && p[x]==0)
16+
x--;
17+
if (x==0) break;
18+
count++;
19+
p[x]--;
20+
21+
while (y+x<=limit && p[y]==0) y++;
22+
if (y+x<=limit && p[y]>0) p[y]--;
2623
}
24+
2725
return count;
2826
}
2927
};

0 commit comments

Comments
 (0)