Skip to content

Commit 6464d9d

Browse files
authored
Update Boats to Save People.java
1 parent c3e1068 commit 6464d9d

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

Medium/Boats to Save People.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
class Solution {
22
public int numRescueBoats(int[] people, int limit) {
33
Arrays.sort(people);
4+
int startIdx = 0;
5+
int endIdx = people.length - 1;
46
int count = 0;
5-
int start = 0;
6-
int end = people.length - 1;
7-
while (end >= start) {
8-
if (people[start] + people[end] <= limit) {
9-
start++;
7+
while (startIdx <= endIdx) {
8+
int currWeight = people[endIdx--];
9+
if (startIdx <= endIdx && currWeight + people[startIdx] <= limit) {
10+
currWeight += people[startIdx++];
1011
}
11-
end--;
1212
count++;
13-
}
13+
}
1414
return count;
15-
}
15+
}
1616
}

0 commit comments

Comments
 (0)