You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/2000-2099/2029.Stone Game IX/README_EN.md
+93-55Lines changed: 93 additions & 55 deletions
Original file line number
Diff line number
Diff line change
@@ -77,7 +77,19 @@ Alice loses the game because the sum of the removed stones (15) is divisible by
77
77
78
78
<!-- solution:start -->
79
79
80
-
### Solution 1
80
+
### Solution 1: Greedy + Case Discussion
81
+
82
+
Since the player's goal is to ensure the total value of the removed stones is not divisible by $3$, we only need to consider the remainder of each stone's value when divided by $3$.
83
+
84
+
We use an array $\textit{cnt}$ of length $3$ to maintain the count of the current remaining stones' values modulo $3$, where $\textit{cnt}[0]$ represents the count of stones with a remainder of $0$, and $\textit{cnt}[1]$ and $\textit{cnt}[2]$ respectively represent the counts of stones with remainders of $1$ and $2$.
85
+
86
+
In the first round, Alice cannot remove stones with a remainder of $0$, as this would make the total value of the removed stones divisible by $3$. Therefore, Alice can only remove stones with a remainder of $1$ or $2$.
87
+
88
+
First, let's consider the case where Alice removes a stone with a remainder of $1$. If Alice removes a stone with a remainder of $1$, the remainder of the total value of stones $0$ against $3$ will not change, so stones with a value remainder of $0$ can be removed in any round, which we will not consider for now. Thus, Bob can only remove stones with a remainder of $1$, followed by Alice removing stones with a remainder of $2$, and so on, in the sequence $1, 1, 2, 1, 2, \ldots$. In this scenario, if the final round is odd and there are still remaining stones, then Alice wins; otherwise, Bob wins.
89
+
90
+
For the case where Alice removes a stone with a remainder of $2$ in the first round, we can draw a similar conclusion.
91
+
92
+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{stones}$. The space complexity is $O(1)$.
81
93
82
94
<!-- tabs:start -->
83
95
@@ -86,47 +98,46 @@ Alice loses the game because the sum of the removed stones (15) is divisible by
0 commit comments