Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6bcdd06

Browse files
committed
Add solution and test-cases for problem 881
1 parent5aa7d84 commit6bcdd06

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

‎leetcode/801-900/0881.Boats-to-Save-People/README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
#[881.Boats to Save People][title]
22

3-
>[!WARNING|style:flat]
4-
>This question is temporarily unanswered if you have good ideas. Welcome to[Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
##Description
4+
You are given an array people where`people[i]` is the weight of the i<sup>th</sup> person, and an**infinite number of boats** where each boat can carry a maximum weight of`limit`. Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most`limit`.
5+
6+
Return the minimum number of boats to carry every given person.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: people = [1,2], limit = 3
12+
Output: 1
13+
Explanation: 1 boat (1, 2)
1314
```
1415

15-
##题意
16-
>...
16+
**Example 2:**
1717

18-
##题解
19-
20-
###思路1
21-
>...
22-
Boats to Save People
23-
```go
18+
```
19+
Input: people = [3,2,2,1], limit = 3
20+
Output: 3
21+
Explanation: 3 boats (1, 2), (2) and (3)
2422
```
2523

24+
**Example 3:**
25+
26+
```
27+
Input: people = [3,5,3,4], limit = 5
28+
Output: 4
29+
Explanation: 4 boats (3), (3), (4), (5)
30+
```
2631

2732
##结语
2833

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package Solution
22

3-
funcSolution(xbool)bool {
4-
returnx
3+
import"sort"
4+
5+
funcSolution(people []int,limitint)int {
6+
sort.Ints(people)
7+
// b + d <= z, 可以得出a+d<=z, 且不可能出现b+c>z的情况
8+
ans:=0
9+
left,right:=0,len(people)-1
10+
forleft<=right {
11+
ans++
12+
ifpeople[left]+people[right]<=limit {
13+
left++
14+
}
15+
right--
16+
}
17+
18+
returnans
519
}

‎leetcode/801-900/0881.Boats-to-Save-People/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
//测试用例
1111
cases:= []struct {
1212
namestring
13-
inputsbool
14-
expectbool
13+
people []int
14+
limitint
15+
expectint
1516
}{
16-
{"TestCase",true,true},
17-
{"TestCase",true,true},
18-
{"TestCase",false,false},
17+
{"TestCase1", []int{1,2},3,1},
18+
{"TestCase2", []int{3,2,2,1},3,3},
1919
}
2020

2121
//开始测试
2222
fori,c:=rangecases {
2323
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
24-
got:=Solution(c.inputs)
24+
got:=Solution(c.people,c.limit)
2525
if!reflect.DeepEqual(got,c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect,got,c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect,got,c.people,c.limit)
2828
}
2929
})
3030
}
3131
}
3232

33-
//压力测试
33+
//压力测试
3434
funcBenchmarkSolution(b*testing.B) {
3535
}
3636

37-
//使用案列
37+
//使用案列
3838
funcExampleSolution() {
3939
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp