1561. Maximum Number of Coins You Can Get Medium

@problem@discussion
#Array#Math#Greedy#Sorting#Game Theory



1/**
2 * [1561] Maximum Number of Coins You Can Get
3 *
4 * There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows:
5 * 
6 * 	In each step, you will choose any 3 piles of coins (not necessarily consecutive).
7 * 	Of your choice, Alice will pick the pile with the maximum number of coins.
8 * 	You will pick the next pile with the maximum number of coins.
9 * 	Your friend Bob will pick the last pile.
10 * 	Repeat until there are no more piles of coins.
11 * 
12 * Given an array of integers piles where piles[i] is the number of coins in the i^th pile.
13 * Return the maximum number of coins that you can have.
14 *  
15 * Example 1:
16 * 
17 * Input: piles = [2,4,1,2,7,8]
18 * Output: 9
19 * 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.
20 * Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one.
21 * The maximum number of coins which you can have are: 7 + 2 = 9.
22 * 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.
23 * 
24 * Example 2:
25 * 
26 * Input: piles = [2,4,5]
27 * Output: 4
28 * 
29 * Example 3:
30 * 
31 * Input: piles = [9,8,7,6,5,1,2,3,4]
32 * Output: 18
33 * 
34 *  
35 * Constraints:
36 * 
37 * 	3 <= piles.length <= 10^5
38 * 	piles.length % 3 == 0
39 * 	1 <= piles[i] <= 10^4
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/maximum-number-of-coins-you-can-get/
45// discuss: https://leetcode.com/problems/maximum-number-of-coins-you-can-get/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn max_coins(piles: Vec<i32>) -> i32 {
51        0
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_1561() {
63    }
64}
65


Back
© 2025 bowen.ge All Rights Reserved.