diff --git a/cpp/2203/220324.cpp b/cpp/2203/220324.cpp new file mode 100644 index 0000000..48126ed --- /dev/null +++ b/cpp/2203/220324.cpp @@ -0,0 +1,43 @@ +#include +#include + +/** + * 881. Boats to Save People + * You are given an array people where people[i] is the weight of the ith person, and an infinite number of boats where each boat can carry a maximum weight of limit. Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit. + * Return the minimum number of boats to carry every given person. + */ + +class Solution { +public: + static int numRescueBoats(const std::vector& people, int limit) { + int cnt[30005]{}; + for (int i : people) + ++cnt[i]; + int L = 0, R = 30004, ret = 0; + for (; L <= R; ++ret) { + while (!cnt[L] && L <= R) + ++L; + while (!cnt[R] && L <= R) + --R; + if (!cnt[R]) + break; + --cnt[R]; + if (cnt[L] && L + R <= limit) + --cnt[L]; + } + return ret; + } +}; + +class SolutionOld { +public: + static int numRescueBoats(std::vector& people, int limit) { + std::sort(people.begin(), people.end()); + int ret = 0, L = 0, R = people.size() - 1; + for (; L <= R; ++ret, --R) { + if (people[R] + people[L] <= limit) + ++L; + } + return ret; + } +}; diff --git a/cpp/2203/CMakeLists.txt b/cpp/2203/CMakeLists.txt index 9621677..d2bdcc1 100644 --- a/cpp/2203/CMakeLists.txt +++ b/cpp/2203/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2203) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2203 220322.cpp) +ADD_EXECUTABLE(2203 220324.cpp)