Skip to content

Commit 3bf7a68

Browse files
committed
day 13
1 parent 914340c commit 3bf7a68

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Boats to Save People
3+
====================
4+
5+
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.
6+
7+
Return the minimum number of boats to carry every given person.
8+
9+
Example 1:
10+
Input: people = [1,2], limit = 3
11+
Output: 1
12+
Explanation: 1 boat (1, 2)
13+
14+
Example 2:
15+
Input: people = [3,2,2,1], limit = 3
16+
Output: 3
17+
Explanation: 3 boats (1, 2), (2) and (3)
18+
19+
Example 3:
20+
Input: people = [3,5,3,4], limit = 5
21+
Output: 4
22+
Explanation: 4 boats (3), (3), (4), (5)
23+
24+
Constraints:
25+
1 <= people.length <= 5 * 104
26+
1 <= people[i] <= limit <= 3 * 104
27+
*/
28+
29+
class Solution
30+
{
31+
public:
32+
int numRescueBoats(vector<int> &people, int limit)
33+
{
34+
sort(people.begin(), people.end());
35+
int ans = 0;
36+
int f = 0, b = people.size() - 1;
37+
38+
while (f <= b)
39+
{
40+
if (f == b)
41+
{
42+
ans++;
43+
f++;
44+
}
45+
else
46+
{
47+
if (people[f] + people[b] <= limit)
48+
{
49+
ans++;
50+
f++;
51+
b--;
52+
}
53+
else
54+
{
55+
ans++;
56+
b--;
57+
}
58+
}
59+
}
60+
61+
return ans;
62+
}
63+
};

Leetcode Daily Challenge/January-2021/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
| 10. | [Create Sorted Array through Instructions](https://leetcode.com/explore/challenge/card/january-leetcoding-challenge-2021/580/week-2-january-8th-january-14th/3599/) | [cpp](./10.%20.cpp) |
1515
| 11. | [Merge Sorted Array](https://leetcode.com/explore/challenge/card/january-leetcoding-challenge-2021/580/week-2-january-8th-january-14th/3600/) | [cpp](./11.%20Merge%20Sorted%20Array.cpp) |
1616
| 12. | [Add Two Numbers](https://leetcode.com/explore/challenge/card/january-leetcoding-challenge-2021/580/week-2-january-8th-january-14th/3601/) | [cpp](./12.%20Add%20Two%20Numbers.cpp) |
17-
| . | []() | [cpp](./.cpp) |
18-
| . | []() | [cpp](./.cpp) |
17+
| 13. | [Boats to Save People](https://leetcode.com/explore/challenge/card/january-leetcoding-challenge-2021/580/week-2-january-8th-january-14th/3602/) | [cpp](./13.%20Boats%20to%20Save%20People.cpp) |
18+
| 14. | []() | [cpp](./14.cpp) |
1919
| . | []() | [cpp](./.cpp) |

0 commit comments

Comments
 (0)