881. Boats to Save People Medium

@problem@discussion
#Array#Two Pointers#Greedy#Sorting



1/**
2 * [881] Boats to Save People
3 *
4 * You are given an array people where people[i] is the weight of the i^th 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.
5 * Return the minimum number of boats to carry every given person.
6 *  
7 * Example 1:
8 * 
9 * Input: people = [1,2], limit = 3
10 * Output: 1
11 * Explanation: 1 boat (1, 2)
12 * 
13 * Example 2:
14 * 
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 * 
21 * Input: people = [3,5,3,4], limit = 5
22 * Output: 4
23 * Explanation: 4 boats (3), (3), (4), (5)
24 * 
25 *  
26 * Constraints:
27 * 
28 * 	1 <= people.length <= 5 * 10^4
29 * 	1 <= people[i] <= limit <= 3 * 10^4
30 * 
31 */
32pub struct Solution {}
33
34// problem: https://leetcode.com/problems/boats-to-save-people/
35// discuss: https://leetcode.com/problems/boats-to-save-people/discuss/?currentPage=1&orderBy=most_votes&query=
36
37// submission codes start here
38
39impl Solution {
40    pub fn num_rescue_boats(people: Vec<i32>, limit: i32) -> i32 {
41        0
42    }
43}
44
45// submission codes end
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_881() {
53    }
54}
55


Back
© 2025 bowen.ge All Rights Reserved.