Skip to content

Commit 2c3e2d3

Browse files
committed
update: 添加问题“881.救生艇”的代码和题解
1 parent 6562eb1 commit 2c3e2d3

11 files changed

+902
-4
lines changed

CHANGELOG.md

Lines changed: 547 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-10 15:16:16
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-10 15:17:50
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int numRescueBoats(vector<int>& people, int limit) {
14+
sort(people.begin(), people.end());
15+
int ans = 0;
16+
for (int l = 0, r = people.size() - 1; l <= r; ans++, r--) {
17+
if (people[l] + people[r] <= limit) {
18+
l++;
19+
}
20+
}
21+
return ans;
22+
}
23+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-10 15:13:13
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-10 15:15:40
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int numRescueBoats(vector<int>& people, int limit) {
14+
sort(people.begin(), people.end());
15+
int ans = 0;
16+
for (int l = 0, r = people.size() - 1; l <= r;) {
17+
if (people[l] + people[r] <= limit) { // 不用特判l是否等于r
18+
l++, r--;
19+
}
20+
else {
21+
r--;
22+
}
23+
ans++;
24+
}
25+
return ans;
26+
}
27+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-10 15:25:02
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-10 15:27:28
6+
*/
7+
package main
8+
9+
import "sort"
10+
11+
func numRescueBoats(people []int, limit int) int {
12+
sort.Ints(people);
13+
ans := 0
14+
for l, r := 0, len(people) - 1; l <= r; ans, r = ans + 1, r - 1 {
15+
if people[l] + people[r] <= limit {
16+
l++;
17+
}
18+
}
19+
return ans
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-10 15:21:02
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-10 15:23:36
6+
*/
7+
import java.util.Arrays;
8+
9+
class Solution {
10+
public int numRescueBoats(int[] people, int limit) {
11+
Arrays.sort(people);
12+
int ans = 0;
13+
for (int l = 0, r = people.length - 1; l <= r; ans++, r--) {
14+
if (people[l] + people[r] <= limit) {
15+
l++;
16+
}
17+
}
18+
return ans;
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-06-10 15:18:38
4+
LastEditors: LetMeFly
5+
LastEditTime: 2024-06-10 15:19:51
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def numRescueBoats(self, people: List[int], limit: int) -> int:
11+
people.sort()
12+
ans = 0
13+
l, r = 0, len(people) - 1
14+
while l <= r:
15+
if people[l] + people[r] <= limit:
16+
l += 1
17+
r -= 1
18+
ans += 1
19+
return ans
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-08 12:06:19
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-08 13:42:22
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
int** visited;
14+
int size;
15+
reference_wrapper<std::vector<int>> a;
16+
17+
int dfs(int l, int r, int val) { // [l, r)
18+
if (l + 1 >= r) {
19+
return 0;
20+
}
21+
if (visited[l][r - 1]) {
22+
return visited[l][r - 1];
23+
}
24+
int ans = 0;
25+
if (a.get()[l] + a.get()[l + 1] == val) {
26+
ans = max(ans, 1 + dfs(l + 2, r, val));
27+
}
28+
if (a.get()[r - 1] + a.get()[r - 2] == val) {
29+
ans = max(ans, 1 + dfs(l, r - 2, val));
30+
}
31+
if (a.get()[l] + a.get()[r - 1] == val) {
32+
ans = max(ans, 1 + dfs(l + 1, r - 1, val));
33+
}
34+
return visited[l][r - 1] = ans;
35+
}
36+
37+
int get(int val) {
38+
// memset(visited, 0, sizeof(visited)); // 报错
39+
// memset(visited, 0, sizeof(int) * size * size); // 报错
40+
for (int i = 0; i < size; i++) {
41+
memset(visited[i], 0, size * sizeof(int));
42+
}
43+
return dfs(0, size, val);
44+
}
45+
public:
46+
Solution() : a(*(new std::vector<int>)) {}
47+
48+
~Solution() {
49+
for (int i = 0; i < size; i++) {
50+
delete[] visited[i];
51+
}
52+
delete[] visited;
53+
}
54+
55+
int maxOperations(vector<int>& nums) {
56+
a = nums;
57+
size = nums.size();
58+
visited = new int*[size];
59+
for (int i = 0; i < size; i++) {
60+
visited[i] = new int[size];
61+
}
62+
return max(get(nums[0] + nums[1]), max(get(nums[size - 1] + nums[size - 2]), get(nums[0] + nums[size - 1])));
63+
}
64+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@
272272
|0873.最长的斐波那契子序列的长度|中等|<a href="https://leetcode.cn/problems/length-of-longest-fibonacci-subsequence/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/09/LeetCode%200873.%E6%9C%80%E9%95%BF%E7%9A%84%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E5%AD%90%E5%BA%8F%E5%88%97%E7%9A%84%E9%95%BF%E5%BA%A6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125690924" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/length-of-longest-fibonacci-subsequence/solution/by-tisfy-4gh4/" target="_blank">LeetCode题解</a>|
273273
|0874.模拟行走机器人|中等|<a href="https://leetcode.cn/problems/walking-robot-simulation/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/19/LeetCode%200874.%E6%A8%A1%E6%8B%9F%E8%A1%8C%E8%B5%B0%E6%9C%BA%E5%99%A8%E4%BA%BA/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131800691" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/walking-robot-simulation/solutions/2349728/letmefly-874mo-ni-xing-zou-ji-qi-ren-ha-90cbo/" target="_blank">LeetCode题解</a>|
274274
|0878.第N个神奇数字|困难|<a href="https://leetcode.cn/problems/nth-magical-number/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/11/22/LeetCode%200878.%E7%AC%ACN%E4%B8%AA%E7%A5%9E%E5%A5%87%E6%95%B0%E5%AD%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127978493" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/nth-magical-number/solutions/1984934/letmefly-878di-n-ge-shen-qi-shu-zi-by-ti-jcvf/" target="_blank">LeetCode题解</a>|
275+
|0881.救生艇|中等|<a href="https://leetcode.cn/problems/boats-to-save-people/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/10/LeetCode%200881.%E6%95%91%E7%94%9F%E8%89%87/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139582336" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/boats-to-save-people/solutions/2806923/letmefly-881jiu-sheng-ting-pai-xu-shuang-ik77/" target="_blank">LeetCode题解</a>|
275276
|0886.可能的二分法|中等|<a href="https://leetcode.cn/problems/possible-bipartition/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/16/LeetCode%200886.%E5%8F%AF%E8%83%BD%E7%9A%84%E4%BA%8C%E5%88%86%E6%B3%95/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127344544" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/possible-bipartition/solution/letmefly-886ke-neng-de-er-fen-fa-tu-sou-zmnxe/" target="_blank">LeetCode题解</a>|
276277
|0889.根据前序和后序遍历构造二叉树|中等|<a href="https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-postorder-traversal/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/02/22/LeetCode%200889.%E6%A0%B9%E6%8D%AE%E5%89%8D%E5%BA%8F%E5%92%8C%E5%90%8E%E5%BA%8F%E9%81%8D%E5%8E%86%E6%9E%84%E9%80%A0%E4%BA%8C%E5%8F%89%E6%A0%91/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136227823" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-postorder-traversal/solutions/2649414/letmefly-889gen-ju-qian-xu-he-hou-xu-bia-9yfs/" target="_blank">LeetCode题解</a>|
277278
|0891.子序列宽度之和|困难|<a href="https://leetcode.cn/problems/sum-of-subsequence-widths/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/11/18/LeetCode%200891.%E5%AD%90%E5%BA%8F%E5%88%97%E5%AE%BD%E5%BA%A6%E4%B9%8B%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127916253" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/sum-of-subsequence-widths/solutions/1977812/letmefly-891zi-xu-lie-kuan-du-zhi-he-by-ttj8r/" target="_blank">LeetCode题解</a>|
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
---
2+
title: 881.救生艇
3+
date: 2024-06-10 21:00:04
4+
tags: [题解, LeetCode, 中等, 贪心, 数组, 双指针, 排序]
5+
---
6+
7+
# 【LetMeFly】881.救生艇:排序+双指针(大人掌船,能捎就捎)
8+
9+
力扣题目链接:[https://leetcode.cn/problems/boats-to-save-people/](https://leetcode.cn/problems/boats-to-save-people/)
10+
11+
<p>给定数组<meta charset="UTF-8" />&nbsp;<code>people</code>&nbsp;。<code>people[i]</code>表示第 <code>i</code><sup>&nbsp;</sup>个人的体重&nbsp;,<strong>船的数量不限</strong>,每艘船可以承载的最大重量为&nbsp;<code>limit</code>。</p>
12+
13+
<p>每艘船最多可同时载两人,但条件是这些人的重量之和最多为&nbsp;<code>limit</code>。</p>
14+
15+
<p>返回 <em>承载所有人所需的最小船数</em>&nbsp;。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<pre>
22+
<strong>输入:</strong>people = [1,2], limit = 3
23+
<strong>输出:</strong>1
24+
<strong>解释:</strong>1 艘船载 (1, 2)
25+
</pre>
26+
27+
<p><strong>示例 2:</strong></p>
28+
29+
<pre>
30+
<strong>输入:</strong>people = [3,2,2,1], limit = 3
31+
<strong>输出:</strong>3
32+
<strong>解释:</strong>3 艘船分别载 (1, 2), (2) 和 (3)
33+
</pre>
34+
35+
<p><strong>示例 3:</strong></p>
36+
37+
<pre>
38+
<strong>输入:</strong>people = [3,5,3,4], limit = 5
39+
<strong>输出:</strong>4
40+
<strong>解释:</strong>4 艘船分别载 (3), (3), (4), (5)</pre>
41+
42+
<p>&nbsp;</p>
43+
44+
<p><strong>提示:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= people.length &lt;= 5 * 10<sup>4</sup></code></li>
48+
<li><code>1 &lt;= people[i] &lt;= limit &lt;= 3 * 10<sup>4</sup></code></li>
49+
</ul>
50+
51+
52+
53+
## 解题方法:排序+双指针
54+
55+
### 具体做法
56+
57+
首先对所有人按体重从低到高排序。
58+
59+
使用两个指针$l$和$r$分别指向数组的第一个和最后一个元素。
60+
61+
每次$r$指针向前移动一个元素(并且救生艇数量加一),另外的,若$r$指针移动前$people[l]+people[r]\leq lmit$,则$l$指针右移一个元素(相当于大人捎个小孩)。
62+
63+
### 为什么这么做
64+
65+
**为什么r指针每次都移动而l指针不一定移动?**
66+
67+
反正每个人都得坐船走,可以理解为先分配大人(每次r左移),若此大人还能捎带一个小孩儿,则就把这个小孩儿带走(此时l右移)。
68+
69+
**大人不应该带上“尽可能重的小孩”吗?**
70+
71+
第一反应是这样的。对于大人$people[r]$,是否应该带上满足$people[i]+people[r]\leq limit$的最大$i$而不是剩下所有小孩中最小的$l$呢?
72+
73+
其实带谁都一样。反正都是一个人头。此时$r$能带上“更重的小孩$i$”,那么待会儿“更轻的大人”也一定能带上小孩$i$。带$l$写起来简单,所以带$l$不带$i$。
74+
75+
### 时空复杂度分析
76+
77+
+ 时间复杂度$O(n\log n)$,其中$n=len(people)$
78+
+ 空间复杂度$O(\log n)$
79+
80+
时空复杂度的来源主要是排序。
81+
82+
### AC代码
83+
84+
#### C++
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
int numRescueBoats(vector<int>& people, int limit) {
90+
sort(people.begin(), people.end());
91+
int ans = 0;
92+
for (int l = 0, r = people.size() - 1; l <= r; ans++, r--) {
93+
// 不用特判l是否等于r,因为不影响结果(若l=r则说明就剩一个人了,带不带那个“虚空影子”都无所谓)
94+
if (people[l] + people[r] <= limit) {
95+
l++;
96+
}
97+
}
98+
return ans;
99+
}
100+
};
101+
```
102+
103+
#### Go
104+
105+
```go
106+
// package main
107+
108+
// import "sort"
109+
110+
func numRescueBoats(people []int, limit int) int {
111+
sort.Ints(people);
112+
ans := 0
113+
for l, r := 0, len(people) - 1; l <= r; ans, r = ans + 1, r - 1 {
114+
if people[l] + people[r] <= limit {
115+
l++;
116+
}
117+
}
118+
return ans
119+
}
120+
```
121+
122+
#### Java
123+
124+
```java
125+
// import java.util.Arrays;
126+
127+
class Solution {
128+
public int numRescueBoats(int[] people, int limit) {
129+
Arrays.sort(people);
130+
int ans = 0;
131+
for (int l = 0, r = people.length - 1; l <= r; ans++, r--) {
132+
if (people[l] + people[r] <= limit) {
133+
l++;
134+
}
135+
}
136+
return ans;
137+
}
138+
}
139+
```
140+
141+
#### Python
142+
143+
```python
144+
# from typing import List
145+
146+
class Solution:
147+
def numRescueBoats(self, people: List[int], limit: int) -> int:
148+
people.sort()
149+
ans = 0
150+
l, r = 0, len(people) - 1
151+
while l <= r:
152+
if people[l] + people[r] <= limit:
153+
l += 1
154+
r -= 1
155+
ans += 1
156+
return ans
157+
```
158+
159+
> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/06/10/LeetCode%200881.%E6%95%91%E7%94%9F%E8%89%87/)哦~
160+
>
161+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/139582336](https://letmefly.blog.csdn.net/article/details/139582336)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ tags: [其他, 知识, 英语, Notes]
191191
|parachute|n. 降落伞<br/>v. 跳伞,空投|
192192
|stripe|n. 条纹,臂章|
193193
|||
194-
|villain|n. 恶棍,反面人物,罪魁祸首|
194+
|<font color="#28bea0" title="二次复习">villain</font>|n. 恶棍,反面人物,罪魁祸首|
195195
|suspense|n. 悬念,焦虑,(对即将发生的事情等)担心,兴奋|
196196
|||
197197
|congestion|n. 充血,淤血,(交通)拥塞,塞车|
@@ -245,7 +245,14 @@ tags: [其他, 知识, 英语, Notes]
245245
|materialize|v. 实现,发生,成为现实,突然显现|
246246
|serene|adj. 平静的,宁静的,安详的<br/>v. 宁静,<诗>使(海,脸色等)平静,使(天空)明朗<br/>n. 平静(的海),<诗,古>晴朗(天空)|
247247
|hazard|n. 危险,危害<br/>冒失地提出,冒险猜测,冒...的风险,使处于危险|
248-
248+
|||
249+
|soot|n. (煤)烟灰,烟垢<br/>v. 用煤炭覆盖|
250+
|parade|v. 游行,队列进行,炫耀,展示<br/>n. (庆祝)游行,队列,阅兵进行,炫耀,展示|
251+
|||
252+
|enchant|v. 使着迷,使陶醉,对...施魔法/念咒语|
253+
|mathematician|n. 数学家|
254+
|inn|n. 客栈,(乡村可夜宿的)小酒店<br/>v. 住旅馆|
255+
|flux|n. 不断的变动,不停的变化,通量,流动|
249256

250257
<details>
251258
<summary>扇贝音频测试</summary>

0 commit comments

Comments
 (0)