add: 230403

This commit is contained in:
Eatswap 2023-04-04 02:17:16 +08:00
parent 599532847a
commit c9be7ea9e7
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 48 additions and 1 deletions

47
cpp/2304/230403.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <algorithm>
#include <iostream>
#include <vector>
/**
* 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<int>& people, int limit) {
std::vector<uint16_t> bucket(30005);
for (int i : people)
++bucket[i];
int ans = bucket[limit], i = 1, j = limit - 1, delta;
for (; i < j; --j) {
if (!bucket[j])
continue;
while (bucket[j] && i + j <= limit && i < j) {
delta = std::min(bucket[j], bucket[i]);
bucket[j] -= delta;
bucket[i] -= delta;
ans += delta;
if (!bucket[i])
++i;
}
if (bucket[j]) {
if (i == j && i + j <= limit)
ans += (bucket[i] >> 1) + (bucket[i] & 1);
else
ans += bucket[j];
}
bucket[j] = 0;
}
if (bucket[i])
ans += (bucket[i] >> 1) + (bucket[i] & 1);
return ans;
}
};
int main() {
std::cout << Solution::numRescueBoats({2,2}, 6);
return 0;
}

View File

@ -4,4 +4,4 @@ PROJECT(2304)
SET(CMAKE_CXX_STANDARD 23)
SET(CMAKE_EXPORT_COMPILE_COMMANDS true)
ADD_EXECUTABLE(2304 230402.cpp)
ADD_EXECUTABLE(2304 230403.cpp)