Skip to content

Commit cc3d268

Browse files
committed
feat: resolved
1 parent 27a1268 commit cc3d268

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

stone_game_vii/src/lib.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,36 @@
11
pub struct Solution {}
22

3+
enum Turn {
4+
AliceTurn,
5+
BobTurn,
6+
}
7+
38
impl Solution {
4-
pub fn stone_game_vii(stones: Vec<i32>) -> i32 {}
9+
fn operate(stones: &[i32], turn: &Turn) -> i32 {
10+
if stones.len() == 0 {
11+
return 0;
12+
}
13+
let next_turn = match turn {
14+
Turn::AliceTurn => Turn::BobTurn,
15+
Turn::BobTurn => Turn::AliceTurn,
16+
};
17+
let stones_without_leftmost = &stones[1..];
18+
let difference_removing_leftmost = Self::operate(stones_without_leftmost, &next_turn); // 如果移动最左边的石头,后续产生的差异
19+
let score_removing_leftmost = stones_without_leftmost.iter().sum::<i32>();
20+
let stones_without_rightmost = &stones[..stones.len() - 1];
21+
let difference_removing_rightmost = Self::operate(stones_without_rightmost, &next_turn);
22+
let score_removing_rightmost = stones_without_rightmost.iter().sum::<i32>();
23+
match turn {
24+
Turn::AliceTurn => (difference_removing_leftmost + score_removing_leftmost)
25+
.max(difference_removing_rightmost + score_removing_rightmost),
26+
Turn::BobTurn => (difference_removing_leftmost - score_removing_leftmost)
27+
.min(difference_removing_rightmost - score_removing_rightmost),
28+
}
29+
}
30+
31+
pub fn stone_game_vii(stones: Vec<i32>) -> i32 {
32+
Self::operate(&stones, &Turn::AliceTurn)
33+
}
534
}
635

736
#[cfg(test)]

0 commit comments

Comments
 (0)