File tree Expand file tree Collapse file tree 1 file changed +30
-1
lines changed Expand file tree Collapse file tree 1 file changed +30
-1
lines changed Original file line number Diff line number Diff line change 1
1
pub struct Solution { }
2
2
3
+ enum Turn {
4
+ AliceTurn ,
5
+ BobTurn ,
6
+ }
7
+
3
8
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
+ }
5
34
}
6
35
7
36
#[ cfg( test) ]
You can’t perform that action at this time.
0 commit comments