Skip to content

Commit

Permalink
Update 881.Boats-to-Save-People_v3.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Dec 4, 2019
1 parent 1a343a2 commit 327fbbb
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions Greedy/881.Boats-to-Save-People/881.Boats-to-Save-People_v3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@ class Solution {
p[w]++;

int count = 0;
int x = limit;
int y = 1;
for (int x = limit; x>0; x--)

while (x>0)
{
for (int i = p[x]; i>0; i--)
{
count++;
p[x]--;

while (y+x<=limit && p[y]==0) y++;
if (y+x<=limit && p[y]>0)
{
p[y]--;
if (x==y) i--;
}

}
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]--;
}

return count;
}
};

0 comments on commit 327fbbb

Please sign in to comment.