-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0881-Boats_to_Save_People.cpp
More file actions
102 lines (92 loc) · 2.74 KB
/
0881-Boats_to_Save_People.cpp
File metadata and controls
102 lines (92 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*******************************************************************************
* 0881-Boats_to_Save_People.cpp
* Billy.Ljm
* 03 Apr 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/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.
*
* ===========
* My Approach
* ===========
* The optimal way is the greedy way: pair the heaviest person with the lightest
* person if possible. Thus, we just have to sort the array, put the heaviest
* person in the boat, and see if the lightest person can be squeezed in. This
* is repeated until all people are in boats, and we have our answer
*
* It has a time complexity of O(n log n) due to sorting, where n is the number
* of people. The space complexity will be O(1), since we'll sort in place and
* mutate the input variable.
******************************************************************************/
#include <iostream>
#include <vector>
#include <algorithm>
class Solution {
public:
/**
* Finds the minimum number of boats needed to save people, given each boat
* can carry at most 2 people and up to a specified weight limit
*
* @param people vector of people's weights
* @param limit weight limit of each boat
*
* @return minimum number of boats needed
*/
int numRescueBoats(std::vector<int>& people, int limit) {
int numboats = 0;
size_t mini = 0, maxi = people.size() - 1; // crawlers
std::sort(people.begin(), people.end(), std::greater<int>());
while (mini <= maxi) {
if (people[mini] + people[maxi] <= limit) {
maxi--;
}
mini++;
numboats++;
}
return numboats;
}
};
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (int i = 0; i < v.size(); i++) {
os << v[i] << ",";
}
os << "\b]";
return os;
}
/**
* Test cases
*/
int main(void) {
Solution sol;
std::vector<int> people;
int limit;
// test case 1
people = { 1,2 };
limit = 3;
std::cout << "numRescueBoats(" << people << ", " << limit << ") = " <<
sol.numRescueBoats(people, limit) << std::endl;
// test case 2
people = { 3,2,2,1 };
limit = 3;
std::cout << "numRescueBoats(" << people << ", " << limit << ") = " <<
sol.numRescueBoats(people, limit) << std::endl;
// test case 3
people = { 3,5,3,4 };
limit = 5;
std::cout << "numRescueBoats(" << people << ", " << limit << ") = " <<
sol.numRescueBoats(people, limit) << std::endl;
return 0;
}