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

Commitfacf8dc

Browse files
add 870
1 parent7203cf7 commitfacf8dc

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ _If you like this project, please leave me a star._ ★
420420
|877|[Stone Game](https://leetcode.com/problems/stone-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_877.java) | |Medium| Math, DP, Minimax
421421
|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|
422422
|872|[Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_872.java) | |Easy| DFS, recursion
423+
|870|[Advantage Shuffle](https://leetcode.com/problems/advantage-shuffle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_870.java) | |Medium|Array, Greedy
423424
|868|[Binary Gap](https://leetcode.com/problems/binary-gap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_868.java)||Easy|
424425
|867|[Transpose Matrix](https://leetcode.com/problems/transpose-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_867.java)||Easy|
425426
|860|[Lemonade Change](https://leetcode.com/problems/lemonade-change/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_860.java)||Easy|
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.Arrays;
5+
importjava.util.Iterator;
6+
importjava.util.List;
7+
8+
publicclass_870 {
9+
publicstaticclassSolution1 {
10+
publicint[]advantageCount(int[]A,int[]B) {
11+
int[]result =newint[A.length];
12+
Arrays.sort(A);
13+
boolean[]used =newboolean[A.length];
14+
for (inti =0;i <A.length;i++) {
15+
result[i] =findSmallestAdvantage(A,used,B[i]);
16+
}
17+
List<Integer>unused =newArrayList<>();
18+
for (inti =0;i <A.length;i++) {
19+
if (!used[i]) {
20+
unused.add(A[i]);
21+
}
22+
}
23+
Iterator<Integer>iterator =unused.iterator();
24+
for (inti =0;i <A.length;i++) {
25+
if (result[i] == -1) {
26+
result[i] =iterator.next();
27+
}
28+
}
29+
returnresult;
30+
}
31+
32+
privateintfindSmallestAdvantage(int[]A,boolean[]used,inttarget) {
33+
for (inti =0;i <A.length;i++) {
34+
if (!used[i] &&A[i] >target) {
35+
used[i] =true;
36+
returnA[i];
37+
}
38+
}
39+
return -1;
40+
}
41+
}
42+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
importcom.fishercoder.solutions._1758;
5+
importcom.fishercoder.solutions._870;
6+
importorg.junit.BeforeClass;
7+
importorg.junit.Test;
8+
9+
importstaticorg.junit.Assert.assertEquals;
10+
11+
publicclass_870Test {
12+
privatestatic_870.Solution1solution1;
13+
14+
@BeforeClass
15+
publicstaticvoidsetup() {
16+
solution1 =new_870.Solution1();
17+
}
18+
19+
@Test
20+
publicvoidtest1() {
21+
CommonUtils.printArray(solution1.advantageCount(newint[]{2,7,11,15},newint[]{1,10,4,11}));
22+
}
23+
24+
@Test
25+
publicvoidtest2() {
26+
CommonUtils.printArray(solution1.advantageCount(newint[]{12,24,8,32},newint[]{13,25,32,11}));
27+
}
28+
29+
@Test
30+
publicvoidtest3() {
31+
CommonUtils.printArray(solution1.advantageCount(newint[]{15,15,4,5,0,1,7,10,3,1,10,10,8,2,3},newint[]{4,13,14,0,14,14,12,3,15,12,2,0,6,9,0}));
32+
}
33+
34+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp