877. Stone Game Medium

@problem@discussion
#Array#Math#Dynamic Programming#Game Theory



1/**
2 * [877] Stone Game
3 *
4 * Alice and Bob play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
5 * The objective of the game is to end with the most stones. The total number of stones across all the piles is odd, so there are no ties.
6 * Alice and Bob take turns, with Alice starting first. Each turn, a player takes the entire pile of stones either from the beginning or from the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
7 * Assuming Alice and Bob play optimally, return true if Alice wins the game, or false if Bob wins.
8 *  
9 * Example 1:
10 * 
11 * Input: piles = [5,3,4,5]
12 * Output: true
13 * Explanation: 
14 * Alice starts first, and can only take the first 5 or the last 5.
15 * Say she takes the first 5, so that the row becomes [3, 4, 5].
16 * If Bob takes 3, then the board is [4, 5], and Alice takes 5 to win with 10 points.
17 * If Bob takes the last 5, then the board is [3, 4], and Alice takes 4 to win with 9 points.
18 * This demonstrated that taking the first 5 was a winning move for Alice, so we return true.
19 * 
20 * Example 2:
21 * 
22 * Input: piles = [3,7,2,3]
23 * Output: true
24 * 
25 *  
26 * Constraints:
27 * 
28 * 	2 <= piles.length <= 500
29 * 	piles.length is even.
30 * 	1 <= piles[i] <= 500
31 * 	sum(piles[i]) is odd.
32 * 
33 */
34pub struct Solution {}
35
36// problem: https://leetcode.com/problems/stone-game/
37// discuss: https://leetcode.com/problems/stone-game/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41impl Solution {
42    pub fn stone_game(piles: Vec<i32>) -> bool {
43        false
44    }
45}
46
47// submission codes end
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_877() {
55    }
56}
57


Back
© 2025 bowen.ge All Rights Reserved.