Skip to content

Commit 0e65c9a

Browse files
committed
一刷1561
1 parent b4681e7 commit 0e65c9a

File tree

5 files changed

+75
-48
lines changed

5 files changed

+75
-48
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10953,12 +10953,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
1095310953
//|Easy
1095410954
//|
1095510955

10956-
//|{counter:codes}
10957-
//|{leetcode_base_url}/maximum-number-of-coins-you-can-get/[1561. Maximum Number of Coins You Can Get^]
10958-
//|{source_base_url}/_1561_MaximumNumberOfCoinsYouCanGet.java[Java]
10959-
//|{doc_base_url}/1561-maximum-number-of-coins-you-can-get.adoc[题解]
10960-
//|Medium
10961-
//|
10956+
|{counter:codes}
10957+
|{leetcode_base_url}/maximum-number-of-coins-you-can-get/[1561. Maximum Number of Coins You Can Get^]
10958+
|{source_base_url}/_1561_MaximumNumberOfCoinsYouCanGet.java[Java]
10959+
|{doc_base_url}/1561-maximum-number-of-coins-you-can-get.adoc[题解]
10960+
|Medium
10961+
|
1096210962

1096310963
//|{counter:codes}
1096410964
//|{leetcode_base_url}/find-latest-group-of-size-m/[1562. Find Latest Group of Size M^]
Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,59 @@
11
[#1561-maximum-number-of-coins-you-can-get]
2-
= 1561. Maximum Number of Coins You Can Get
2+
= 1561. 你可以获得的最大硬币数目
33

4-
{leetcode}/problems/maximum-number-of-coins-you-can-get/[LeetCode - 1561. Maximum Number of Coins You Can Get ^]
4+
https://leetcode.cn/problems/maximum-number-of-coins-you-can-get/[LeetCode - 1561. 你可以获得的最大硬币数目 ^]
55

6-
There are `3n` piles of coins of varying size, you and your friends will take piles of coins as follows:
6+
有 3n 堆数目不一的硬币,你和你的朋友们打算按以下方式分硬币:
77

8+
* 每一轮中,你将会选出 *任意* 3 堆硬币(不一定连续)。
9+
* Alice 将会取走硬币数量最多的那一堆。
10+
* 你将会取走硬币数量第二多的那一堆。
11+
* Bob 将会取走最后一堆。
12+
* 重复这个过程,直到没有更多硬币。
813
9-
* In each step, you will choose *any *`3` piles of coins (not necessarily consecutive).
10-
* Of your choice, Alice will pick the pile with the maximum number of coins.
11-
* You will pick the next pile with the maximum number of coins.
12-
* Your friend Bob will pick the last pile.
13-
* Repeat until there are no more piles of coins.
14+
给你一个整数数组 `piles` ,其中 `piles[i]` 是第 `i` 堆中硬币的数目。
1415

16+
返回你可以获得的最大硬币数目。
1517

16-
Given an array of integers `piles` where `piles[i]` is the number of coins in the `i^th^` pile.
1718

18-
Return the maximum number of coins that you can have.
19+
*示例 1:*
1920

20-
21-
*Example 1:*
21+
....
22+
输入:piles = [2,4,1,2,7,8]
23+
输出:9
24+
解释:选出 (2, 7, 8) ,Alice 取走 8 枚硬币的那堆,你取走 7 枚硬币的那堆,Bob 取走最后一堆。
25+
选出 (1, 2, 4) , Alice 取走 4 枚硬币的那堆,你取走 2 枚硬币的那堆,Bob 取走最后一堆。
26+
你可以获得的最大硬币数目:7 + 2 = 9.
27+
考虑另外一种情况,如果选出的是 (1, 2, 8) 和 (2, 4, 7) ,你就只能得到 2 + 4 = 6 枚硬币,这不是最优解。
28+
....
2229

23-
[subs="verbatim,quotes"]
24-
----
25-
*Input:* piles = [2,4,1,2,7,8]
26-
*Output:* 9
27-
*Explanation: *Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with *7* coins and Bob the last one.
28-
Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with *2* coins and Bob the last one.
29-
The maximum number of coins which you can have are: 7 + 2 = 9.
30-
On the other hand if we choose this arrangement (1, *2*, 8), (2, *4*, 7) you only get 2 + 4 = 6 coins which is not optimal.
31-
----
30+
*示例 2:*
3231

33-
*Example 2:*
32+
....
33+
输入:piles = [2,4,5]
34+
输出:4
35+
....
3436

35-
[subs="verbatim,quotes"]
36-
----
37-
*Input:* piles = [2,4,5]
38-
*Output:* 4
39-
----
37+
*示例 3:*
4038

41-
*Example 3:*
39+
....
40+
输入:piles = [9,8,7,6,5,1,2,3,4]
41+
输出:18
42+
....
4243

43-
[subs="verbatim,quotes"]
44-
----
45-
*Input:* piles = [9,8,7,6,5,1,2,3,4]
46-
*Output:* 18
47-
----
4844

49-
50-
*Constraints:*
45+
*提示:*
5146

52-
53-
* `3 <= piles.length <= 10^5^`
47+
* `3 \<= piles.length <= 10^5^`
5448
* `piles.length % 3 == 0`
55-
* `1 <= piles[i] <= 10^4^`
56-
57-
49+
* `1 \<= piles[i] \<= 10^4^`
5850
5951
6052
== 思路分析
6153

54+
因为每次最高都被 Alice 拿走,想要获取最多硬币,那就要取一个尽可能靠近最大值的数量,对数组排序,从后面取两个,从前面取一个,这样中间的值之和就可以取到最大的值。
55+
56+
TIP: 把下标写出来,更容易找出规律。可以让条件写的更简单。
6257

6358
[[src-1561]]
6459
[tabs]
@@ -85,4 +80,5 @@ include::{sourcedir}/_1561_MaximumNumberOfCoinsYouCanGet.java[tag=answer]
8580

8681
== 参考资料
8782

88-
83+
. https://leetcode.cn/problems/maximum-number-of-coins-you-can-get/solutions/409109/ni-ke-yi-huo-de-de-zui-da-ying-bi-shu-mu-by-leetco/[1561. 你可以获得的最大硬币数目 - 官方题解^]
84+
. https://leetcode.cn/problems/maximum-number-of-coins-you-can-get/solutions/3035907/tan-xin-jian-ji-xie-fa-pythonjavaccgojsr-2ptn/[1561. 你可以获得的最大硬币数目 - 贪心,简洁写法^]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3205,7 +3205,7 @@ include::1530-number-of-good-leaf-nodes-pairs.adoc[leveloffset=+1]
32053205

32063206
// include::1560-most-visited-sector-in-a-circular-track.adoc[leveloffset=+1]
32073207

3208-
// include::1561-maximum-number-of-coins-you-can-get.adoc[leveloffset=+1]
3208+
include::1561-maximum-number-of-coins-you-can-get.adoc[leveloffset=+1]
32093209

32103210
// include::1562-find-latest-group-of-size-m.adoc[leveloffset=+1]
32113211

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,11 @@ endif::[]
783783
|{doc_base_url}/1410-html-entity-parser.adoc[题解]
784784
|✅ 哈希 + 字符串
785785

786+
|{counter:codes2503}
787+
|{leetcode_base_url}/maximum-number-of-coins-you-can-get/[1561. 你可以获得的最大硬币数目^]
788+
|{doc_base_url}/1561-maximum-number-of-coins-you-can-get.adoc[题解]
789+
|✅ 贪心。每次去第二大的数字即可。
790+
786791
|===
787792

788793
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.Arrays;
4+
5+
public class _1561_MaximumNumberOfCoinsYouCanGet {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2025-05-22 23:28:11
10+
*/
11+
public int maxCoins(int[] piles) {
12+
Arrays.sort(piles);
13+
int result = 0;
14+
// TODO 有更简单的写法,尝试把坐标写出来,去寻找规律。
15+
for (int i = piles.length - 2, cnt = piles.length / 3;
16+
cnt > 0 && i > 0; i -= 2, cnt--) {
17+
result += piles[i];
18+
}
19+
return result;
20+
}
21+
// end::answer[]
22+
public static void main(String[] args) {
23+
new _1561_MaximumNumberOfCoinsYouCanGet()
24+
.maxCoins(new int[]{2, 4, 1, 2, 7, 8});
25+
}
26+
}

0 commit comments

Comments
 (0)