3847. Find the Score Difference in a Game Medium

@problem@discussion
#Array#Simulation



1/**
2 * [3847] Find the Score Difference in a Game
3 *
4 * You are given an integer array nums, where nums[i] represents the points scored in the i^th game.
5 * There are exactly two players. Initially, the first player is active and the second player is inactive.
6 * The following rules apply sequentially for each game i:
7 * 
8 * 	If nums[i] is odd, the active and inactive players swap roles.
9 * 	In every 6th game (that is, game indices 5, 11, 17, ...), the active and inactive players swap roles.
10 * 	The active player plays the i^th game and gains nums[i] points.
11 * 
12 * Return the score difference, defined as the first player's total score minus the second player's total score.
13 *  
14 * <strong class="example">Example 1:
15 * <div class="example-block">
16 * Input: <span class="example-io">nums = [1,2,3]</span>
17 * Output: <span class="example-io">0</span>
18 * Explanation:​​​​​​​
19 * 
20 * 	Game 0: Since the points are odd, the second player becomes active and gains nums[0] = 1 point.
21 * 	Game 1: No swap occurs. The second player gains nums[1] = 2 points.
22 * 	Game 2: Since the points are odd, the first player becomes active and gains nums[2] = 3 points.
23 * 	The score difference is 3 - 3 = 0.
24 * </div>
25 * <strong class="example">Example 2:
26 * <div class="example-block">
27 * Input: <span class="example-io">nums = [2,4,2,1,2,1]</span>
28 * Output: <span class="example-io">4</span>
29 * Explanation:
30 * 
31 * 	Games 0 to 2: The first player gains 2 + 4 + 2 = 8 points.
32 * 	Game 3: Since the points are odd, the second player is now active and gains nums[3] = 1 point.
33 * 	Game 4: The second player gains nums[4] = 2 points.
34 * 	Game 5: Since the points are odd, the players swap roles. Then, because this is the 6th game, the players swap again. The second player gains nums[5] = 1 point.
35 * 	The score difference is 8 - 4 = 4.
36 * </div>
37 * <strong class="example">Example 3:
38 * <div class="example-block">
39 * Input: <span class="example-io">nums = [1]</span>
40 * Output: <span class="example-io">-1</span>
41 * Explanation:
42 * 
43 * 	Game 0: Since the points are odd, the second player is now active and gains nums[0] = 1 point.
44 * 	The score difference is 0 - 1 = -1.
45 * </div>
46 *  
47 * Constraints:
48 * 
49 * 	1 <= nums.length <= 1000
50 * 	1 <= nums[i] <= 1000
51 * 
52 */
53pub struct Solution {}
54
55// problem: https://leetcode.com/problems/find-the-score-difference-in-a-game/
56// discuss: https://leetcode.com/problems/find-the-score-difference-in-a-game/discuss/?currentPage=1&orderBy=most_votes&query=
57
58// submission codes start here
59
60impl Solution {
61    pub fn score_difference(nums: Vec<i32>) -> i32 {
62        0
63    }
64}
65
66// submission codes end
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    #[test]
73    fn test_3847() {
74    }
75}
76

Back
© 2026 bowen.ge All Rights Reserved.