Skip to content

Commit facf8dc

Browse files
add 870
1 parent 7203cf7 commit facf8dc

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+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Iterator;
6+
import java.util.List;
7+
8+
public class _870 {
9+
public static class Solution1 {
10+
public int[] advantageCount(int[] A, int[] B) {
11+
int[] result = new int[A.length];
12+
Arrays.sort(A);
13+
boolean[] used = new boolean[A.length];
14+
for (int i = 0; i < A.length; i++) {
15+
result[i] = findSmallestAdvantage(A, used, B[i]);
16+
}
17+
List<Integer> unused = new ArrayList<>();
18+
for (int i = 0; i < A.length; i++) {
19+
if (!used[i]) {
20+
unused.add(A[i]);
21+
}
22+
}
23+
Iterator<Integer> iterator = unused.iterator();
24+
for (int i = 0; i < A.length; i++) {
25+
if (result[i] == -1) {
26+
result[i] = iterator.next();
27+
}
28+
}
29+
return result;
30+
}
31+
32+
private int findSmallestAdvantage(int[] A, boolean[] used, int target) {
33+
for (int i = 0; i < A.length; i++) {
34+
if (!used[i] && A[i] > target) {
35+
used[i] = true;
36+
return A[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+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._1758;
5+
import com.fishercoder.solutions._870;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class _870Test {
12+
private static _870.Solution1 solution1;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _870.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
CommonUtils.printArray(solution1.advantageCount(new int[]{2, 7, 11, 15}, new int[]{1, 10, 4, 11}));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
CommonUtils.printArray(solution1.advantageCount(new int[]{12, 24, 8, 32}, new int[]{13, 25, 32, 11}));
27+
}
28+
29+
@Test
30+
public void test3() {
31+
CommonUtils.printArray(solution1.advantageCount(new int[]{15, 15, 4, 5, 0, 1, 7, 10, 3, 1, 10, 10, 8, 2, 3}, new int[]{4, 13, 14, 0, 14, 14, 12, 3, 15, 12, 2, 0, 6, 9, 0}));
32+
}
33+
34+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy