Skip to content

Commit 6f39615

Browse files
authored
feat: add weekly contest 490 (#5042)
1 parent 8a23e10 commit 6f39615

35 files changed

Lines changed: 2637 additions & 3 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
- [乘积小于 K 的子数组](/solution/0700-0799/0713.Subarray%20Product%20Less%20Than%20K/README.md) - `双指针`
5353
- [位 1 的个数](/solution/0100-0199/0191.Number%20of%201%20Bits/README.md) - `位运算``lowbit`
5454
- [合并区间](/solution/0000-0099/0056.Merge%20Intervals/README.md) - `区间合并`
55-
<!-- 排序算法、待补充 -->
55+
<!-- 排序算法、待补充 -->
5656

5757
### 2. 数据结构
5858

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3847.Find%20the%20Score%20Difference%20in%20a%20Game/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3847. 计算比赛分数差](https://leetcode.cn/problems/find-the-score-difference-in-a-game)
10+
11+
[English Version](/solution/3800-3899/3847.Find%20the%20Score%20Difference%20in%20a%20Game/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个整数数组 <code>nums</code>,其中 <code>nums[i]</code> 表示在第 <code>i</code>&nbsp;场比赛中获得的分数。</p>
18+
19+
<p><strong>恰好 </strong>有两位玩家。初始时,第一位玩家为<strong>&nbsp;主动玩家</strong>,第二位玩家为&nbsp;<strong>被动玩家</strong>。</p>
20+
21+
<p><strong>按顺序</strong>&nbsp;将下述规则应用于每场比赛 <code>i</code>:</p>
22+
23+
<ul>
24+
<li>如果 <code>nums[i]</code> 是奇数,主动玩家和被动玩家互换角色。</li>
25+
<li>在每第 6 场比赛(即比赛索引为 <code>5, 11, 17, ...</code> 的比赛中),主动玩家和被动玩家互换角色。</li>
26+
<li>主动玩家参与第 <code>i</code>&nbsp;场比赛,并获得 <code>nums[i]</code> 分。</li>
27+
</ul>
28+
29+
<p>返回<strong>&nbsp;分数差</strong>,即第一位玩家的&nbsp;<strong>总分&nbsp;</strong>减去第二位玩家的&nbsp;<strong>总分&nbsp;</strong>。</p>
30+
31+
<p>&nbsp;</p>
32+
33+
<p><strong class="example">示例 1:</strong></p>
34+
35+
<div class="example-block">
36+
<p><strong>输入:</strong> <span class="example-io">nums = [1,2,3]</span></p>
37+
38+
<p><strong>输出:</strong> <span class="example-io">0</span></p>
39+
40+
<p><strong>解释:</strong>​​​​​​​</p>
41+
42+
<ul>
43+
<li>第 0 场比赛:分数为奇数,第二位玩家成为主动玩家,获得 <code>nums[0] = 1</code> 分。</li>
44+
<li>第 1 场比赛:没有交换角色。第二位玩家获得 <code>nums[1] = 2</code> 分。</li>
45+
<li>第 2 场比赛:分数为奇数,第一位玩家成为主动玩家,获得 <code>nums[2] = 3</code> 分。</li>
46+
<li>分数差为 <code>3 - 3 = 0</code>。</li>
47+
</ul>
48+
</div>
49+
50+
<p><strong class="example">示例 2:</strong></p>
51+
52+
<div class="example-block">
53+
<p><strong>输入:</strong> <span class="example-io">nums = [2,4,2,1,2,1]</span></p>
54+
55+
<p><strong>输出:</strong> <span class="example-io">4</span></p>
56+
57+
<p><strong>解释:</strong></p>
58+
59+
<ul>
60+
<li>第 0 到第 2 场比赛:第一位玩家获得 <code>2 + 4 + 2 = 8</code> 分。</li>
61+
<li>第 3 场比赛:分数为奇数,第二位玩家成为主动玩家,获得 <code>nums[3] = 1</code> 分。</li>
62+
<li>第 4 场比赛:第二位玩家获得 <code>nums[4] = 2</code> 分。</li>
63+
<li>第 5 场比赛:分数为奇数,玩家互换角色。由于这是第 6 场比赛,玩家再次互换角色。第二位玩家获得 <code>nums[5] = 1</code> 分。</li>
64+
<li>分数差为 <code>8 - 4 = 4</code>。</li>
65+
</ul>
66+
</div>
67+
68+
<p><strong class="example">示例 3:</strong></p>
69+
70+
<div class="example-block">
71+
<p><strong>输入:</strong> <span class="example-io">nums = [1]</span></p>
72+
73+
<p><strong>输出:</strong> <span class="example-io">-1</span></p>
74+
75+
<p><strong>解释:</strong></p>
76+
77+
<ul>
78+
<li>第 0 场比赛:分数为奇数,第二位玩家成为主动玩家,获得 <code>nums[0] = 1</code> 分。</li>
79+
<li>分数差为 <code>0 - 1 = -1</code>。</li>
80+
</ul>
81+
</div>
82+
83+
<p>&nbsp;</p>
84+
85+
<p><strong>提示:</strong></p>
86+
87+
<ul>
88+
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
89+
<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>
90+
</ul>
91+
92+
<!-- description:end -->
93+
94+
## 解法
95+
96+
<!-- solution:start -->
97+
98+
### 方法一:模拟
99+
100+
我们用一个变量 $k$ 来表示当前玩家的角色,初始时 $k = 1$,当 $k = 1$ 时表示第一位玩家是主动玩家,当 $k = -1$ 时表示第二位玩家是主动玩家。对于每场比赛,我们根据题目描述更新 $k$ 的值,并将当前比赛的分数乘以 $k$ 加到答案中。最后返回答案即可。
101+
102+
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
103+
104+
<!-- tabs:start -->
105+
106+
#### Python3
107+
108+
```python
109+
class Solution:
110+
def scoreDifference(self, nums: List[int]) -> int:
111+
ans, k = 0, 1
112+
for i, x in enumerate(nums):
113+
if x % 2:
114+
k *= -1
115+
if i % 6 == 5:
116+
k *= -1
117+
ans += k * x
118+
return ans
119+
```
120+
121+
#### Java
122+
123+
```java
124+
class Solution {
125+
public int scoreDifference(int[] nums) {
126+
int ans = 0;
127+
int k = 1;
128+
for (int i = 0; i < nums.length; ++i) {
129+
int x = nums[i];
130+
if ((x & 1) == 1) {
131+
k = -k;
132+
}
133+
if (i % 6 == 5) {
134+
k = -k;
135+
}
136+
ans += k * x;
137+
}
138+
return ans;
139+
}
140+
}
141+
```
142+
143+
#### C++
144+
145+
```cpp
146+
class Solution {
147+
public:
148+
int scoreDifference(vector<int>& nums) {
149+
int ans = 0;
150+
int k = 1;
151+
for (int i = 0; i < nums.size(); ++i) {
152+
int x = nums[i];
153+
if (x & 1) {
154+
k = -k;
155+
}
156+
if (i % 6 == 5) {
157+
k = -k;
158+
}
159+
ans += k * x;
160+
}
161+
return ans;
162+
}
163+
};
164+
```
165+
166+
#### Go
167+
168+
```go
169+
func scoreDifference(nums []int) int {
170+
ans := 0
171+
k := 1
172+
for i, x := range nums {
173+
if x%2 != 0 {
174+
k = -k
175+
}
176+
if i%6 == 5 {
177+
k = -k
178+
}
179+
ans += k * x
180+
}
181+
return ans
182+
}
183+
```
184+
185+
#### TypeScript
186+
187+
```ts
188+
function scoreDifference(nums: number[]): number {
189+
let ans = 0;
190+
let k = 1;
191+
192+
nums.forEach((x, i) => {
193+
if (x % 2 !== 0) {
194+
k = -k;
195+
}
196+
if (i % 6 === 5) {
197+
k = -k;
198+
}
199+
ans += k * x;
200+
});
201+
202+
return ans;
203+
}
204+
```
205+
206+
<!-- tabs:end -->
207+
208+
<!-- solution:end -->
209+
210+
<!-- problem:end -->

0 commit comments

Comments
 (0)