File tree Expand file tree Collapse file tree 1 file changed +65
-0
lines changed
Others/2029.Stone-Game-IX Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool stoneGameIX (vector<int >& stones)
4
+ {
5
+ vector<int >count (3 );
6
+ for (int x: stones)
7
+ count[x%3 ]+=1 ;
8
+
9
+ auto temp = count;
10
+ if (temp[1 ]>0 )
11
+ {
12
+ temp[1 ]-=1 ;
13
+ if (!win (temp, 1 , 1 ))
14
+ return true ;
15
+ }
16
+
17
+ temp = count;
18
+ if (temp[2 ]>0 )
19
+ {
20
+ temp[2 ]-=1 ;
21
+ if (!win (temp, 2 , 1 ))
22
+ return true ;
23
+ }
24
+
25
+ return false ;
26
+ }
27
+
28
+ bool win (vector<int >&count, int sum, int turn)
29
+ {
30
+ if (count[0 ]+count[1 ]+count[2 ]==0 )
31
+ {
32
+ if (turn == 1 )
33
+ return true ;
34
+ else
35
+ return false ;
36
+ }
37
+
38
+ if (count[0 ]>0 )
39
+ {
40
+ count[0 ]-=1 ;
41
+ return 1 -win (count, sum, 1 -turn);
42
+ }
43
+
44
+ if (sum%3 ==1 )
45
+ {
46
+ if (count[1 ]>0 )
47
+ {
48
+ count[1 ]--;
49
+ return 1 -win (count, sum+1 , 1 -turn);
50
+ }
51
+ else
52
+ return false ;
53
+ }
54
+ else
55
+ {
56
+ if (count[2 ]>0 )
57
+ {
58
+ count[2 ]--;
59
+ return 1 - win (count, sum+2 , 1 -turn);
60
+ }
61
+ else
62
+ return false ;
63
+ }
64
+ }
65
+ };
You can’t perform that action at this time.
0 commit comments