Skip to content

Commit ef6f730

Browse files
authored
Create 881. Boats to Save People 3 apr
881. Boats to Save People
1 parent df41224 commit ef6f730

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

881. Boats to Save People 3 apr

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
class Solution {
3+
public:
4+
int numRescueBoats(vector<int>& people, int limit) {
5+
6+
// sort vector
7+
sort(people.begin(),people.end());
8+
9+
int i = 0, j = people.size() - 1,cnt = 0;
10+
11+
while(i <= j)
12+
{
13+
14+
if(people[i] + people[j] <= limit)
15+
{
16+
++i;
17+
--j;
18+
}
19+
// if sum is over the limit,
20+
// heaviest will go alone.
21+
else
22+
--j;
23+
24+
++cnt; // number of boats
25+
}
26+
27+
return cnt;
28+
29+
}
30+
31+
};

0 commit comments

Comments
 (0)