Skip to content

Commit 12961cd

Browse files
add 881
1 parent a223214 commit 12961cd

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ _If you like this project, please leave me a star._ ★
362362
|885|[Spiral Matrix III](https://leetcode.com/problems/spiral-matrix-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_885.java) |[:tv:](https://www.youtube.com/watch?v=0qep3f9cqVs) |Medium|
363363
|884|[Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_884.java) | |Easy|
364364
|883|[Projection Area of 3D Shapes](https://leetcode.com/problems/projection-area-of-3d-shapes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_883.java) | |Easy|Math
365+
|881|[Boats to Save People](https://leetcode.com/problems/boats-to-save-people/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_881.java) | |Medium|Two Pointers, Greedy
365366
|880|[Decoded String at Index](https://leetcode.com/problems/decoded-string-at-index/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_880.java) | |Medium|Stack
366367
|876|[Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_876.java) | |Easy|
367368
|872|[Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_872.java) | |Easy| DFS, recursion
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.TreeMap;
6+
7+
public class _881 {
8+
public static class Solution1 {
9+
public int numRescueBoats(int[] people, int limit) {
10+
TreeMap<Integer, Integer> map = new TreeMap<>();
11+
for (int w : people) {
12+
map.put(w, map.getOrDefault(w, 0) + 1);
13+
}
14+
int boats = 0;
15+
List<Integer> uniqWeights = new ArrayList(map.keySet());
16+
int left = 0;
17+
int right = uniqWeights.size() - 1;
18+
while (left < right) {
19+
int heavierWeight = uniqWeights.get(right);
20+
int lighterWeight = uniqWeights.get(left);
21+
if (heavierWeight + lighterWeight <= limit) {
22+
int pairs = Math.min(map.get(heavierWeight), map.get(lighterWeight));
23+
boats += pairs;
24+
if (map.get(heavierWeight) == pairs && map.get(lighterWeight) == pairs) {
25+
map.remove(heavierWeight);
26+
map.remove(lighterWeight);
27+
left++;
28+
right--;
29+
} else if (map.get(heavierWeight) == pairs) {
30+
map.remove(heavierWeight);
31+
map.put(lighterWeight, map.get(lighterWeight) - pairs);
32+
right--;
33+
} else {
34+
map.remove(lighterWeight);
35+
map.put(heavierWeight, map.get(heavierWeight) - pairs);
36+
left++;
37+
}
38+
} else {
39+
boats += map.get(heavierWeight);
40+
map.remove(heavierWeight);
41+
right--;
42+
}
43+
}
44+
if (!map.isEmpty()) {
45+
int weight = uniqWeights.get(left);
46+
int remainingPeople = map.get(weight);
47+
if (remainingPeople == 1) {
48+
boats++;
49+
} else {
50+
if (weight * 2 <= limit) {
51+
boats += (remainingPeople / 2 + ((remainingPeople % 2 == 0) ? 0 : 1));
52+
} else {
53+
boats += remainingPeople;
54+
}
55+
}
56+
}
57+
return boats;
58+
}
59+
}
60+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._881;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _881Test {
10+
private static _881.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _881.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(1, solution1.numRescueBoats(new int[]{1, 2}, 3));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(3, solution1.numRescueBoats(new int[]{3, 2, 2, 1}, 3));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(4, solution1.numRescueBoats(new int[]{3, 5, 3, 4}, 5));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(2, solution1.numRescueBoats(new int[]{2, 4}, 5));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(29, solution1.numRescueBoats(new int[]{4, 9, 3, 1, 1, 7, 6, 10, 10, 10, 1, 8, 8, 7, 8, 10, 7, 4, 6, 3, 6, 1, 2, 4, 8, 8, 4, 7, 1, 2, 10, 3, 4, 6, 3, 5, 3, 1, 2, 6, 1, 5, 4, 5, 1, 10, 5, 9, 10, 4}, 10));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)