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

Commit12961cd

Browse files
add 881
1 parenta223214 commit12961cd

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+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.List;
5+
importjava.util.TreeMap;
6+
7+
publicclass_881 {
8+
publicstaticclassSolution1 {
9+
publicintnumRescueBoats(int[]people,intlimit) {
10+
TreeMap<Integer,Integer>map =newTreeMap<>();
11+
for (intw :people) {
12+
map.put(w,map.getOrDefault(w,0) +1);
13+
}
14+
intboats =0;
15+
List<Integer>uniqWeights =newArrayList(map.keySet());
16+
intleft =0;
17+
intright =uniqWeights.size() -1;
18+
while (left <right) {
19+
intheavierWeight =uniqWeights.get(right);
20+
intlighterWeight =uniqWeights.get(left);
21+
if (heavierWeight +lighterWeight <=limit) {
22+
intpairs =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+
}elseif (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+
intweight =uniqWeights.get(left);
46+
intremainingPeople =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+
returnboats;
58+
}
59+
}
60+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._881;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_881Test {
10+
privatestatic_881.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_881.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(1,solution1.numRescueBoats(newint[]{1,2},3));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(3,solution1.numRescueBoats(newint[]{3,2,2,1},3));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals(4,solution1.numRescueBoats(newint[]{3,5,3,4},5));
30+
}
31+
32+
@Test
33+
publicvoidtest4() {
34+
assertEquals(2,solution1.numRescueBoats(newint[]{2,4},5));
35+
}
36+
37+
@Test
38+
publicvoidtest5() {
39+
assertEquals(29,solution1.numRescueBoats(newint[]{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)

[8]ページ先頭

©2009-2025 Movatter.jp