1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00
LeetCode/cpp/boats-to-save-people.cpp
Matej Focko b229608723
cpp(chore): add clang-format style and format
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-01-03 12:06:54 +01:00

26 lines
663 B
C++

#include <algorithm>
#include <vector>
class Solution {
public:
int numRescueBoats(std::vector<int> people, int limit) {
std::sort(people.begin(), people.end());
int counter = 0;
std::size_t left = 0, right = people.size() - 1;
while (left <= right && left < people.size() && right < people.size()) {
counter++;
// if they both it, shift the left pointer
if (left < right && people[left] + people[right] <= limit) {
left++;
}
// anyways, at least the heaviest person is included
right--;
}
return counter;
}
};