-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBoats_881.java
More file actions
30 lines (27 loc) · 839 Bytes
/
Boats_881.java
File metadata and controls
30 lines (27 loc) · 839 Bytes
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
import java.util.Arrays;
public class Boats_881 {
public int numRescueBoats(int[] people, int limit) {
Arrays.sort(people);
int i, j;
for (i = 0, j = people.length - 1; i <= j; j--)
if (people[i] + people[j] <= limit) i++;
return people.length - 1 - j;
}
// Time: O(n), Space: O(n)
/*
public int numRescueBoats(int[] people, int limit) {
int[] buckets = new int[limit + 1];
for (int weight : people) buckets[weight]++;
int i = 1, j = limit, res = 0;
while (i <= j) {
while (i <= j && buckets[i] <= 0) i++;
while (i <= j && buckets[j] <= 0) j--;
if (i > j) return res;
res++;
if (i + j <= limit) buckets[i]--;
buckets[j]--;
}
return res;
}
*/
}