Skip to content

Commit ecd89b9

Browse files
add 1561
1 parent 5355c0f commit ecd89b9

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1561|[Maximum Number of Coins You Can Get](https://leetcode.com/problems/maximum-number-of-coins-you-can-get/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1561.java) ||Sort|Medium|
1112
|1560|[Most Visited Sector in a Circular Track](https://leetcode.com/problems/most-visited-sector-in-a-circular-track/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1560.java) ||Array|Easy|
1213
|1558|[Minimum Numbers of Function Calls to Make Target Array](https://leetcode.com/problems/minimum-numbers-of-function-calls-to-make-target-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1558.java) ||Medium|Greedy|
1314
|1557|[Minimum Number of Vertices to Reach All Nodes](https://leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1557.java) ||Medium|Graph|
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
5+
public class _1561 {
6+
public static class Solution1 {
7+
public int maxCoins(int[] piles) {
8+
Arrays.sort(piles);
9+
int j = 0;
10+
int coins = 0;
11+
for (int i = piles.length - 2; i > 0; i -= 2) {
12+
coins += piles[i];
13+
if (++j == piles.length / 3) {
14+
return coins;
15+
}
16+
}
17+
return coins;
18+
}
19+
}
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1561;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1561Test {
10+
private static _1561.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1561.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(9, solution1.maxCoins(new int[]{2, 4, 1, 2, 7, 8}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(4, solution1.maxCoins(new int[]{2, 4, 5}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(18, solution1.maxCoins(new int[]{9, 8, 7, 6, 5, 1, 2, 3, 4}));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)