Skip to content

Commit 0b700ba

Browse files
committed
added problem 881
1 parent 8b43515 commit 0b700ba

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

problems/problem0881/life_boats.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package problem0881
2+
3+
import "sort"
4+
5+
/*
6+
You are given an array people where people[i] is the weight of the ith person,
7+
and an infinite number of boats where each boat can carry a maximum weight of limit.
8+
Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit.
9+
Return the minimum number of boats to carry every given person.
10+
*/
11+
12+
func numRescueBoats(people []int, limit int) int {
13+
var light, heavy, res int
14+
// Sort from lightest to heaviest
15+
sort.Ints(people)
16+
// Start from both ends and approach the middle
17+
for heavy = len(people) - 1; light <= heavy; res++ {
18+
if people[light]+people[heavy] <= limit {
19+
// If both the current heaviest an current lightest can
20+
// go in the same boat, pair them up
21+
light++
22+
heavy--
23+
} else {
24+
// If not, heaviest will go alone
25+
heavy--
26+
}
27+
}
28+
return res
29+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package problem0881
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type Result struct {
11+
Input []int
12+
Limit int
13+
Expected int
14+
}
15+
16+
var Results = []Result{
17+
{[]int{1, 2}, 3, 1},
18+
{[]int{3, 2, 2, 1}, 3, 3},
19+
{[]int{3, 5, 3, 4}, 5, 4},
20+
}
21+
22+
func TestLifeBoats(t *testing.T) {
23+
assert := assert.New(t)
24+
25+
for _, res := range Results {
26+
want := res.Expected
27+
temp := make([]int, len(res.Input))
28+
copy(temp, res.Input)
29+
got := numRescueBoats(temp, res.Limit)
30+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
31+
}
32+
}

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ Each problem is in it's own directory, with test files. There are helper package
275275
| 0872 | [Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees) | [My Solution](./problems/problem0872) ||
276276
| 0875 | [Koko Eating Bananas](https://leetcode.com/problems/koko-eating-bananas) | [My Solution](./problems/problem0875) ||
277277
| 0876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list) | [My Solution](./problems/problem0876) ||
278+
| 0881 | [Boats to Save People](https://leetcode.com/problems/boats-to-save-people) | [My Solution](./problems/problem0881) ||
278279
| 0885 | [Spiral Matrix III](https://leetcode.com/problems/spiral-matrix-iii) | [My Solution](./problems/problem0885) ||
279280
| 0886 | [Possible Bipartition](https://leetcode.com/problems/possible-bipartition) | [My Solution](./problems/problem0886) ||
280281
| 0888 | [Fair Candy Swap](https://leetcode.com/problems/fair-candy-swap) | [My Solution](./problems/problem0888) ||

0 commit comments

Comments
 (0)