Skip to content

Commit 2da48fd

Browse files
authored
Create Boats to Save People.cpp
1 parent 58d2fd4 commit 2da48fd

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Leet Code/Boats to Save People.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* Leet Code */
2+
/* Title - Boats to Save People */
3+
/* Created By - Akash Modak */
4+
/* Date - 09/06/2023 */
5+
6+
// 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.
7+
8+
// Return the minimum number of boats to carry every given person.
9+
10+
11+
12+
// Example 1:
13+
14+
// Input: people = [1,2], limit = 3
15+
// Output: 1
16+
// Explanation: 1 boat (1, 2)
17+
// Example 2:
18+
19+
// Input: people = [3,2,2,1], limit = 3
20+
// Output: 3
21+
// Explanation: 3 boats (1, 2), (2) and (3)
22+
// Example 3:
23+
24+
// Input: people = [3,5,3,4], limit = 5
25+
// Output: 4
26+
// Explanation: 4 boats (3), (3), (4), (5)
27+
28+
class Solution {
29+
public:
30+
int numRescueBoats(vector<int>& people, int limit) {
31+
sort(people.begin(), people.end());
32+
int i=0,j=people.size()-1,count=0;
33+
while(i<=j){
34+
if(people[i]+people[j]<=limit)
35+
i++;
36+
j--;
37+
count++;
38+
}
39+
return count;
40+
}
41+
};

0 commit comments

Comments
 (0)