Skip to content

Commit 8a4adfd

Browse files
committed
feat: add solutions to lc problem: No.0881. Boats to Save People
1 parent 3e85172 commit 8a4adfd

File tree

4 files changed

+75
-25
lines changed

4 files changed

+75
-25
lines changed

solution/0800-0899/0881.Boats to Save People/README.md

+27-2
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,47 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49+
“排序 + 双指针”实现。
50+
4951
<!-- tabs:start -->
5052

5153
### **Python3**
5254

5355
<!-- 这里可写当前语言的特殊实现逻辑 -->
5456

5557
```python
56-
58+
class Solution:
59+
def numRescueBoats(self, people: List[int], limit: int) -> int:
60+
people.sort()
61+
num, i, j = 0, 0, len(people) - 1
62+
while i <= j:
63+
if people[i] + people[j] <= limit:
64+
i += 1
65+
j -= 1
66+
num += 1
67+
return num
5768
```
5869

5970
### **Java**
6071

6172
<!-- 这里可写当前语言的特殊实现逻辑 -->
6273

6374
```java
64-
75+
class Solution {
76+
public int numRescueBoats(int[] people, int limit) {
77+
Arrays.sort(people);
78+
int num = 0;
79+
int i = 0, j = people.length - 1;
80+
while (i <= j) {
81+
if (people[i] + people[j] <= limit) {
82+
++i;
83+
}
84+
--j;
85+
++num;
86+
}
87+
return num;
88+
}
89+
}
6590
```
6691

6792
### **...**

solution/0800-0899/0881.Boats to Save People/README_EN.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,36 @@
4949
### **Python3**
5050

5151
```python
52-
52+
class Solution:
53+
def numRescueBoats(self, people: List[int], limit: int) -> int:
54+
people.sort()
55+
num, i, j = 0, 0, len(people) - 1
56+
while i <= j:
57+
if people[i] + people[j] <= limit:
58+
i += 1
59+
j -= 1
60+
num += 1
61+
return num
5362
```
5463

5564
### **Java**
5665

5766
```java
58-
67+
class Solution {
68+
public int numRescueBoats(int[] people, int limit) {
69+
Arrays.sort(people);
70+
int num = 0;
71+
int i = 0, j = people.length - 1;
72+
while (i <= j) {
73+
if (people[i] + people[j] <= limit) {
74+
++i;
75+
}
76+
--j;
77+
++num;
78+
}
79+
return num;
80+
}
81+
}
5982
```
6083

6184
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int numRescueBoats(int[] people, int limit) {
3+
Arrays.sort(people);
4+
int num = 0;
5+
int i = 0, j = people.length - 1;
6+
while (i <= j) {
7+
if (people[i] + people[j] <= limit) {
8+
++i;
9+
}
10+
--j;
11+
++num;
12+
}
13+
return num;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
11
class Solution:
2-
def numRescueBoats(self, people, limit):
3-
"""
4-
:type people: List[int]
5-
:type limit: int
6-
:rtype: int
7-
"""
2+
def numRescueBoats(self, people: List[int], limit: int) -> int:
83
people.sort()
9-
left = 0
10-
right = len(people) - 1
11-
ans = 0
12-
while left < right:
13-
if people[left] + people[right] <= limit:
14-
ans += 1
15-
left += 1
16-
right -= 1
17-
else:
18-
ans += 1
19-
right -= 1
20-
else:
21-
if left == right:
22-
ans += 1
23-
return ans
4+
num, i, j = 0, 0, len(people) - 1
5+
while i <= j:
6+
if people[i] + people[j] <= limit:
7+
i += 1
8+
j -= 1
9+
num += 1
10+
return num

0 commit comments

Comments
 (0)