Boats to Save People LeetCode solution in C++

1
// ��������Please upvote if it helps ��������
2
class Solution {
3
public:
4
int numRescueBoats(vector<int> &people, int limit) {
5
// sort vector
6
sort(people.begin(), people.end());
7

8
int i = 0, j = people.size() - 1, cnt = 0;
9

10
while (i <= j) {
11
// lightest person + heaviest person sum <= limit
12
// they can go together
13
if (people[i] + people[j] <= limit) {
14
++i;
15
--j;
16
}
17
// if sum is over the limit,
18
// heaviest will go alone.
19
else
20
--j;
21

22
++cnt; // number of boats
23
}
24

25
return cnt;
26
}
27
// for github repository link go to my profile.
28
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0