276. Boats to Save People
MediumTwo PointersGreedy
Each boat carries at most two people with total weight ≤ limit. Print the minimum number of boats.
Input format
Line 1: n limit Line 2: n weights (each ≤ limit)
Output format
The minimum boats.
Example 1
Input
2 3 1 2
Output
1
Example 2
Input
4 3 3 2 2 1
Output
3
Constraints
1 ≤ n ≤ 10⁵
Read input from standard input and print the answer to standard output. Your code is judged against 5 test cases, most of them hidden.
Loading the MyLoginn IDE…