Skip to content

Commit ec01a10

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

File tree

10 files changed

+137
-37
lines changed

10 files changed

+137
-37
lines changed

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

+48-10
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53-
“排序 + 双指针”实现。
53+
**方法一:贪心 + 双指针**
54+
55+
排序后,使用双指针分别指向数组首尾,每次取两个指针指向的元素之和与 `limit` 比较,如果小于等于 `limit`,则两个指针同时向中间移动一位,否则只移动右指针。累加答案即可。
56+
57+
时间复杂度 $O(n\log n)$,其中 $n$ 为数组 `people` 的长度。
5458

5559
<!-- tabs:start -->
5660

@@ -62,13 +66,14 @@
6266
class Solution:
6367
def numRescueBoats(self, people: List[int], limit: int) -> int:
6468
people.sort()
65-
num, i, j = 0, 0, len(people) - 1
69+
ans = 0
70+
i, j = 0, len(people) - 1
6671
while i <= j:
6772
if people[i] + people[j] <= limit:
6873
i += 1
6974
j -= 1
70-
num += 1
71-
return num
75+
ans += 1
76+
return ans
7277
```
7378

7479
### **Java**
@@ -79,17 +84,50 @@ class Solution:
7984
class Solution {
8085
public int numRescueBoats(int[] people, int limit) {
8186
Arrays.sort(people);
82-
int num = 0;
83-
int i = 0, j = people.length - 1;
84-
while (i <= j) {
87+
int ans = 0;
88+
for (int i = 0, j = people.length - 1; i <= j; --j) {
89+
if (people[i] + people[j] <= limit) {
90+
++i;
91+
}
92+
++ans;
93+
}
94+
return ans;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int numRescueBoats(vector<int>& people, int limit) {
105+
sort(people.begin(), people.end());
106+
int ans = 0;
107+
for (int i = 0, j = people.size() - 1; i <= j; --j) {
85108
if (people[i] + people[j] <= limit) {
86109
++i;
87110
}
88-
--j;
89-
++num;
111+
++ans;
90112
}
91-
return num;
113+
return ans;
92114
}
115+
};
116+
```
117+
118+
### **Go**
119+
120+
```go
121+
func numRescueBoats(people []int, limit int) int {
122+
sort.Ints(people)
123+
ans := 0
124+
for i, j := 0, len(people)-1; i <= j; j-- {
125+
if people[i]+people[j] <= limit {
126+
i++
127+
}
128+
ans++
129+
}
130+
return ans
93131
}
94132
```
95133

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

+43-9
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@
5151
class Solution:
5252
def numRescueBoats(self, people: List[int], limit: int) -> int:
5353
people.sort()
54-
num, i, j = 0, 0, len(people) - 1
54+
ans = 0
55+
i, j = 0, len(people) - 1
5556
while i <= j:
5657
if people[i] + people[j] <= limit:
5758
i += 1
5859
j -= 1
59-
num += 1
60-
return num
60+
ans += 1
61+
return ans
6162
```
6263

6364
### **Java**
@@ -66,20 +67,53 @@ class Solution:
6667
class Solution {
6768
public int numRescueBoats(int[] people, int limit) {
6869
Arrays.sort(people);
69-
int num = 0;
70-
int i = 0, j = people.length - 1;
71-
while (i <= j) {
70+
int ans = 0;
71+
for (int i = 0, j = people.length - 1; i <= j; --j) {
7272
if (people[i] + people[j] <= limit) {
7373
++i;
7474
}
75-
--j;
76-
++num;
75+
++ans;
7776
}
78-
return num;
77+
return ans;
7978
}
8079
}
8180
```
8281

82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
int numRescueBoats(vector<int>& people, int limit) {
88+
sort(people.begin(), people.end());
89+
int ans = 0;
90+
for (int i = 0, j = people.size() - 1; i <= j; --j) {
91+
if (people[i] + people[j] <= limit) {
92+
++i;
93+
}
94+
++ans;
95+
}
96+
return ans;
97+
}
98+
};
99+
```
100+
101+
### **Go**
102+
103+
```go
104+
func numRescueBoats(people []int, limit int) int {
105+
sort.Ints(people)
106+
ans := 0
107+
for i, j := 0, len(people)-1; i <= j; j-- {
108+
if people[i]+people[j] <= limit {
109+
i++
110+
}
111+
ans++
112+
}
113+
return ans
114+
}
115+
```
116+
83117
### **...**
84118

85119
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int numRescueBoats(vector<int>& people, int limit) {
4+
sort(people.begin(), people.end());
5+
int ans = 0;
6+
for (int i = 0, j = people.size() - 1; i <= j; --j) {
7+
if (people[i] + people[j] <= limit) {
8+
++i;
9+
}
10+
++ans;
11+
}
12+
return ans;
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func numRescueBoats(people []int, limit int) int {
2+
sort.Ints(people)
3+
ans := 0
4+
for i, j := 0, len(people)-1; i <= j; j-- {
5+
if people[i]+people[j] <= limit {
6+
i++
7+
}
8+
ans++
9+
}
10+
return ans
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
class Solution {
22
public int numRescueBoats(int[] people, int limit) {
33
Arrays.sort(people);
4-
int num = 0;
5-
int i = 0, j = people.length - 1;
6-
while (i <= j) {
4+
int ans = 0;
5+
for (int i = 0, j = people.length - 1; i <= j; --j) {
76
if (people[i] + people[j] <= limit) {
87
++i;
98
}
10-
--j;
11-
++num;
9+
++ans;
1210
}
13-
return num;
11+
return ans;
1412
}
1513
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
class Solution:
22
def numRescueBoats(self, people: List[int], limit: int) -> int:
33
people.sort()
4-
num, i, j = 0, 0, len(people) - 1
4+
ans = 0
5+
i, j = 0, len(people) - 1
56
while i <= j:
67
if people[i] + people[j] <= limit:
78
i += 1
89
j -= 1
9-
num += 1
10-
return num
10+
ans += 1
11+
return ans

solution/2400-2499/2441.Largest Positive Integer That Exists With Its Negative/README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53-
**方法一:枚举**
53+
**方法一:哈希表 + 枚举**
54+
55+
我们先用哈希表 $s$ 记录数组中的所有元素,然后枚举数组中的每个元素 $x$,如果 $x$ 为正数,且 $-x$ 在哈希表 $s$ 中,更新答案。
56+
57+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组的长度。
5458

5559
<!-- tabs:start -->
5660

solution/CONTEST_README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
| 段位 | 比例 | 段位名 | 国服分数线 | 勋章 |
1313
| ----- | ------ | -------- | --------- | --------------------------------------------------------------------------- |
14-
| LV3 | 5% | Guardian | &ge;2221.96 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
15-
| LV2 | 20% | Knight | &ge;1869.96 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
14+
| LV3 | 5% | Guardian | &ge;2225.82 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
15+
| LV2 | 20% | Knight | &ge;1871.95 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
1616
| LV1 | 75% | - | - | - |
1717

1818
力扣竞赛 **全国排名前 10** 的用户,全站用户名展示为品牌橙色。

solution/CONTEST_README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ If you are in the top 25% of the contest rating, you’ll get the “Knight” b
1313

1414
| Level | Proportion | Badge | Rating | |
1515
| ----- | ---------- | ---------- | -------------- | ----------------------------------------------------------------------------------------------------------------------- |
16-
| LV3 | 5\% | Guardian | &ge;2175.86 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
17-
| LV2 | 20\% | Knight | &ge;1850.05 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
16+
| LV3 | 5\% | Guardian | &ge;2178.58 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
17+
| LV2 | 20\% | Knight | &ge;1851.23 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
1818
| LV1 | 75\% | - | - | - |
1919

2020
For top 10 users (excluding LCCN users), your LeetCode ID will be colored orange on the ranking board. You'll also have the honor with you when you post/comment under discuss.

solution/contest.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ def generate_contest_list():
127127
128128
| 段位 | 比例 | 段位名 | 国服分数线 | 勋章 |
129129
| ----- | ------ | -------- | --------- | --------------------------------------------------------------------------- |
130-
| LV3 | 5% | Guardian | &ge;2221.96 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
131-
| LV2 | 20% | Knight | &ge;1869.96 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
130+
| LV3 | 5% | Guardian | &ge;2225.82 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
131+
| LV2 | 20% | Knight | &ge;1871.95 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
132132
| LV1 | 75% | - | - | - |
133133
134134
力扣竞赛 **全国排名前 10** 的用户,全站用户名展示为品牌橙色。
@@ -153,8 +153,8 @@ def generate_contest_list():
153153
154154
| Level | Proportion | Badge | Rating | |
155155
| ----- | ---------- | ---------- | -------------- | ----------------------------------------------------------------------------------------------------------------------- |
156-
| LV3 | 5\% | Guardian | &ge;2175.86 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
157-
| LV2 | 20\% | Knight | &ge;1850.05 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
156+
| LV3 | 5\% | Guardian | &ge;2178.58 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
157+
| LV2 | 20\% | Knight | &ge;1851.23 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
158158
| LV1 | 75\% | - | - | - |
159159
160160
For top 10 users (excluding LCCN users), your LeetCode ID will be colored orange on the ranking board. You'll also have the honor with you when you post/comment under discuss.

0 commit comments

Comments
 (0)