Skip to content

Commit 5df74a7

Browse files
committed
add 881
1 parent 3ae0b64 commit 5df74a7

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

boats-to-save-people.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import "sort"
4+
5+
// 881 https://leetcode-cn.com/problems/boats-to-save-people/
6+
// 贪心
7+
func numRescueBoats(people []int, limit int) (ans int) {
8+
sort.Ints(people)
9+
10+
light, heavy := 0, len(people)-1
11+
for light <= heavy {
12+
if people[heavy] == limit {
13+
ans++
14+
heavy--
15+
continue
16+
}
17+
if people[light]+people[heavy] > limit {
18+
heavy--
19+
} else {
20+
heavy--
21+
light++
22+
}
23+
ans++
24+
}
25+
26+
return
27+
}

0 commit comments

Comments
 (0)