Skip to content

Commit a7154eb

Browse files
committed
一刷881
1 parent 7011810 commit a7154eb

File tree

5 files changed

+65
-15
lines changed

5 files changed

+65
-15
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6192,14 +6192,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
61926192
//|{doc_base_url}/0880-decoded-string-at-index.adoc[题解]
61936193
//|Medium
61946194
//|
6195-
//
6196-
//|{counter:codes}
6197-
//|{leetcode_base_url}/boats-to-save-people/[881. Boats to Save People^]
6198-
//|{source_base_url}/_0881_BoatsToSavePeople.java[Java]
6199-
//|{doc_base_url}/0881-boats-to-save-people.adoc[题解]
6200-
//|Medium
6201-
//|
6202-
//
6195+
6196+
|{counter:codes}
6197+
|{leetcode_base_url}/boats-to-save-people/[881. Boats to Save People^]
6198+
|{source_base_url}/_0881_BoatsToSavePeople.java[Java]
6199+
|{doc_base_url}/0881-boats-to-save-people.adoc[题解]
6200+
|Medium
6201+
|
6202+
62036203
//|{counter:codes}
62046204
//|{leetcode_base_url}/reachable-nodes-in-subdivided-graph/[882. Reachable Nodes In Subdivided Graph^]
62056205
//|{source_base_url}/_0882_ReachableNodesInSubdividedGraph.java[Java]

docs/0881-boats-to-save-people.adoc

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ Each boat carries at most 2 people at the same time, provided the sum of the wei
99

1010
Return the minimum number of boats to carry every given person. (It is guaranteed each person can be carried by a boat.)
1111

12-
13-
1412

1513
*Example 1:*
1614

@@ -46,18 +44,39 @@ Return the minimum number of boats to carry every given person. (It is guarante
4644
*Note*:
4745

4846

49-
* `1 <= people.length <= 50000`
50-
* `1 <= people[i] <= limit <= 30000`
51-
52-
47+
* `1 \<= people.length \<= 50000`
48+
* `1 \<= people[i] \<= limit \<= 30000`
5349
5450
51+
== 思路分析
5552

53+
对数组排序,从两端向中间挤压。
5654

55+
WARNING: 注意审题!一条小船只能坐两个人。
5756

5857
[[src-0881]]
58+
[tabs]
59+
====
60+
一刷::
61+
+
62+
--
5963
[{java_src_attr}]
6064
----
6165
include::{sourcedir}/_0881_BoatsToSavePeople.java[tag=answer]
6266
----
67+
--
68+
69+
// 二刷::
70+
// +
71+
// --
72+
// [{java_src_attr}]
73+
// ----
74+
// include::{sourcedir}/_0881_BoatsToSavePeople_2.java[tag=answer]
75+
// ----
76+
// --
77+
====
78+
79+
== 参考资料
80+
81+
. https://leetcode.cn/problems/boats-to-save-people/solutions/958838/jiu-sheng-ting-by-leetcode-solution-0nsp/[881. 救生艇 - 官方题解^]
6382

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,7 @@ include::0876-middle-of-the-linked-list.adoc[leveloffset=+1]
18361836

18371837
// include::0880-decoded-string-at-index.adoc[leveloffset=+1]
18381838

1839-
// include::0881-boats-to-save-people.adoc[leveloffset=+1]
1839+
include::0881-boats-to-save-people.adoc[leveloffset=+1]
18401840

18411841
// include::0882-reachable-nodes-in-subdivided-graph.adoc[leveloffset=+1]
18421842

logbook/202406.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,12 @@
904904
|⭕️ 理解题目,确定符合要求的条件。
905905
906906
907+
|{counter:codes}
908+
|{leetcode_base_url}/boats-to-save-people/[881. Boats to Save People^]
909+
|{doc_base_url}/0881-boats-to-save-people.adoc[题解]
910+
|❌ 贪心+双指针
911+
912+
907913
|===
908914
909915
截止目前,本轮练习一共完成 {codes} 道题。
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.Arrays;
4+
5+
public class _0881_BoatsToSavePeople {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2024-09-15 23:20:35
10+
*/
11+
public int numRescueBoats(int[] people, int limit) {
12+
Arrays.sort(people);
13+
int result = 0;
14+
int light = 0, heavy = people.length - 1;
15+
while (light <= heavy) {
16+
if (people[light] + people[heavy] < limit) {
17+
light++;
18+
}
19+
heavy--;
20+
result++;
21+
}
22+
return result;
23+
}
24+
// end::answer[]
25+
}

0 commit comments

Comments
 (0)