-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoatCounter.java
More file actions
40 lines (34 loc) · 844 Bytes
/
BoatCounter.java
File metadata and controls
40 lines (34 loc) · 844 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
31
32
33
34
35
36
37
38
39
40
package org.sean.array;
import java.util.Arrays;
/***
* 881. Boats to Save People
*/
public class BoatCounter {
public int numRescueBoats(int[] people, int limit) {
// 2 pointers
Arrays.sort(people);
int len = people.length;
int left = 0, right = len - 1;
int cnt = 0;
while (left < right) {
int l = people[left];
int r = people[right];
if (r >= limit) {
right--;
cnt++;
} else {
if (l + r <= limit) {
right--;
left++;
cnt++;
} else { // >
right--;
cnt++;
}
}
}
if (left == right)
cnt++;
return cnt;
}
}