Skip to content

Commit 937aaf4

Browse files
committed
Add solution #1561
1 parent 6bf13b1 commit 937aaf4

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,369 LeetCode solutions in JavaScript
1+
# 1,370 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1192,6 +1192,7 @@
11921192
1558|[Minimum Numbers of Function Calls to Make Target Array](./solutions/1558-minimum-numbers-of-function-calls-to-make-target-array.js)|Medium|
11931193
1559|[Detect Cycles in 2D Grid](./solutions/1559-detect-cycles-in-2d-grid.js)|Medium|
11941194
1560|[Most Visited Sector in a Circular Track](./solutions/1560-most-visited-sector-in-a-circular-track.js)|Easy|
1195+
1561|[Maximum Number of Coins You Can Get](./solutions/1561-maximum-number-of-coins-you-can-get.js)|Medium|
11951196
1566|[Detect Pattern of Length M Repeated K or More Times](./solutions/1566-detect-pattern-of-length-m-repeated-k-or-more-times.js)|Easy|
11961197
1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](./solutions/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js)|Medium|
11971198
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 1561. Maximum Number of Coins You Can Get
3+
* https://leetcode.com/problems/maximum-number-of-coins-you-can-get/
4+
* Difficulty: Medium
5+
*
6+
* There are 3n piles of coins of varying size, you and your friends will take piles of coins as
7+
* follows:
8+
* - In each step, you will choose any 3 piles of coins (not necessarily consecutive).
9+
* - Of your choice, Alice will pick the pile with the maximum number of coins.
10+
* - You will pick the next pile with the maximum number of coins.
11+
* - Your friend Bob will pick the last pile.
12+
* - Repeat until there are no more piles of coins.
13+
*
14+
* Given an array of integers piles where piles[i] is the number of coins in the ith pile.
15+
*
16+
* Return the maximum number of coins that you can have.
17+
*/
18+
19+
/**
20+
* @param {number[]} piles
21+
* @return {number}
22+
*/
23+
var maxCoins = function(piles) {
24+
piles.sort((a, b) => b - a);
25+
let result = 0;
26+
const rounds = piles.length / 3;
27+
28+
for (let i = 1; i < piles.length - rounds; i += 2) {
29+
result += piles[i];
30+
}
31+
32+
return result;
33+
};

0 commit comments

Comments
 (0)