Skip to content

Commit 41233e3

Browse files
authored
feat: add weekly contest 446 (#4362)
1 parent 0d0ad7c commit 41233e3

File tree

18 files changed

+1438
-1
lines changed

18 files changed

+1438
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3522.Calculate%20Score%20After%20Performing%20Instructions/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3522. 执行指令后的得分](https://leetcode.cn/problems/calculate-score-after-performing-instructions)
10+
11+
[English Version](/solution/3500-3599/3522.Calculate%20Score%20After%20Performing%20Instructions/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你两个数组:<code>instructions</code> 和 <code>values</code>,数组的长度均为 <code>n</code>。</p>
18+
19+
<p>你需要根据以下规则模拟一个过程:</p>
20+
21+
<ul>
22+
<li>从下标&nbsp;<code>i = 0</code> 的第一个指令开始,初始得分为 0。</li>
23+
<li>如果 <code>instructions[i]</code> 是 <code>"add"</code>:
24+
<ul>
25+
<li>将 <code>values[i]</code> 加到你的得分中。</li>
26+
<li>移动到下一个指令 <code>(i + 1)</code>。</li>
27+
</ul>
28+
</li>
29+
<li>如果 <code>instructions[i]</code> 是 <code>"jump"</code>:
30+
<ul>
31+
<li>移动到下标为&nbsp;<code>(i + values[i])</code> 的指令,但不修改你的得分。</li>
32+
</ul>
33+
</li>
34+
</ul>
35+
36+
<p>当以下任一情况发生时,过程会终止:</p>
37+
38+
<ul>
39+
<li>越界(即 <code>i &lt; 0</code> 或 <code>i &gt;= n</code>),或</li>
40+
<li>尝试再次执行已经执行过的指令。被重复访问的指令不会再次执行。</li>
41+
</ul>
42+
43+
<p>返回过程结束时的得分。</p>
44+
45+
<p>&nbsp;</p>
46+
47+
<p><strong class="example">示例 1:</strong></p>
48+
49+
<div class="example-block">
50+
<p><strong>输入:</strong> <span class="example-io">instructions = ["jump","add","add","jump","add","jump"], values = [2,1,3,1,-2,-3]</span></p>
51+
52+
<p><strong>输出:</strong> <span class="example-io">1</span></p>
53+
54+
<p><strong>解释:</strong></p>
55+
56+
<p>从下标&nbsp;0 开始模拟过程:</p>
57+
58+
<ul>
59+
<li>下标 0:指令是 <code>"jump"</code>,移动到下标&nbsp;<code>0 + 2 = 2</code>。</li>
60+
<li>下标 2:指令是 <code>"add"</code>,将 <code>values[2] = 3</code> 加到得分中,移动到下标&nbsp;3。得分变为 3。</li>
61+
<li>下标 3:指令是 <code>"jump"</code>,移动到下标&nbsp;<code>3 + 1 = 4</code>。</li>
62+
<li>下标 4:指令是 <code>"add"</code>,将 <code>values[4] = -2</code> 加到得分中,移动到下标&nbsp;5。得分变为 1。</li>
63+
<li>下标 5:指令是 <code>"jump"</code>,移动到下标&nbsp;<code>5 + (-3) = 2</code>。</li>
64+
<li>下标 2:已经访问过。过程结束。</li>
65+
</ul>
66+
</div>
67+
68+
<p><strong class="example">示例 2:</strong></p>
69+
70+
<div class="example-block">
71+
<p><strong>输入:</strong> <span class="example-io">instructions = ["jump","add","add"], values = [3,1,1]</span></p>
72+
73+
<p><strong>输出:</strong> <span class="example-io">0</span></p>
74+
75+
<p><strong>解释:</strong></p>
76+
77+
<p>从下标&nbsp;0 开始模拟过程:</p>
78+
79+
<ul>
80+
<li>下标 0:指令是 <code>"jump"</code>,移动到下标&nbsp;<code>0 + 3 = 3</code>。</li>
81+
<li>下标 3:越界。过程结束。</li>
82+
</ul>
83+
</div>
84+
85+
<p><strong class="example">示例 3:</strong></p>
86+
87+
<div class="example-block">
88+
<p><strong>输入:</strong> <span class="example-io">instructions = ["jump"], values = [0]</span></p>
89+
90+
<p><strong>输出:</strong> <span class="example-io">0</span></p>
91+
92+
<p><strong>解释:</strong></p>
93+
94+
<p>从下标&nbsp;0 开始模拟过程:</p>
95+
96+
<ul>
97+
<li>下标 0:指令是 <code>"jump"</code>,移动到下标&nbsp;<code>0 + 0 = 0</code>。</li>
98+
<li>下标 0:已经访问过。过程结束。</li>
99+
</ul>
100+
</div>
101+
102+
<p>&nbsp;</p>
103+
104+
<p><strong>提示:</strong></p>
105+
106+
<ul>
107+
<li><code>n == instructions.length == values.length</code></li>
108+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
109+
<li><code>instructions[i]</code> 只能是 <code>"add"</code> 或 <code>"jump"</code>。</li>
110+
<li><code>-10<sup>5</sup> &lt;= values[i] &lt;= 10<sup>5</sup></code></li>
111+
</ul>
112+
113+
<!-- description:end -->
114+
115+
## 解法
116+
117+
<!-- solution:start -->
118+
119+
### 方法一:模拟
120+
121+
我们根据题意模拟即可。
122+
123+
我们定义一个长度为 $n$ 的布尔数组 $\textit{vis}$,用于记录每一条指令是否被执行过,初始时均为 $\text{false}$。
124+
125+
然后我们从下标 $i = 0$ 开始,循环执行以下操作:
126+
127+
1. 将 $\textit{vis}[i]$ 置为 $\text{true}$;
128+
2. 如果 $\textit{instructions}[i]$ 的第一个字符为 'a',那么我们将答案增加 $\textit{value}[i]$,然后 $i$ 加 $1$;否则,我们将 $i$ 增加 $\textit{value}[i]$。
129+
130+
循环,直至 $i \lt 0$ 或者 $i \ge n$,或者 $\textit{vis}[i]$ 为 $\text{true}$。
131+
132+
最后返回答案即可。
133+
134+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{value}$ 的长度。
135+
136+
<!-- tabs:start -->
137+
138+
#### Python3
139+
140+
```python
141+
class Solution:
142+
def calculateScore(self, instructions: List[str], values: List[int]) -> int:
143+
n = len(values)
144+
vis = [False] * n
145+
ans = i = 0
146+
while 0 <= i < n and not vis[i]:
147+
vis[i] = True
148+
if instructions[i][0] == "a":
149+
ans += values[i]
150+
i += 1
151+
else:
152+
i = i + values[i]
153+
return ans
154+
```
155+
156+
#### Java
157+
158+
```java
159+
class Solution {
160+
public long calculateScore(String[] instructions, int[] values) {
161+
int n = values.length;
162+
boolean[] vis = new boolean[n];
163+
long ans = 0;
164+
int i = 0;
165+
166+
while (i >= 0 && i < n && !vis[i]) {
167+
vis[i] = true;
168+
if (instructions[i].charAt(0) == 'a') {
169+
ans += values[i];
170+
i += 1;
171+
} else {
172+
i = i + values[i];
173+
}
174+
}
175+
176+
return ans;
177+
}
178+
}
179+
```
180+
181+
#### C++
182+
183+
```cpp
184+
class Solution {
185+
public:
186+
long long calculateScore(vector<string>& instructions, vector<int>& values) {
187+
int n = values.size();
188+
vector<bool> vis(n, false);
189+
long long ans = 0;
190+
int i = 0;
191+
192+
while (i >= 0 && i < n && !vis[i]) {
193+
vis[i] = true;
194+
if (instructions[i][0] == 'a') {
195+
ans += values[i];
196+
i += 1;
197+
} else {
198+
i += values[i];
199+
}
200+
}
201+
202+
return ans;
203+
}
204+
};
205+
```
206+
207+
#### Go
208+
209+
```go
210+
func calculateScore(instructions []string, values []int) (ans int64) {
211+
n := len(values)
212+
vis := make([]bool, n)
213+
i := 0
214+
for i >= 0 && i < n && !vis[i] {
215+
vis[i] = true
216+
if instructions[i][0] == 'a' {
217+
ans += int64(values[i])
218+
i += 1
219+
} else {
220+
i += values[i]
221+
}
222+
}
223+
return
224+
}
225+
```
226+
227+
#### TypeScript
228+
229+
```ts
230+
function calculateScore(instructions: string[], values: number[]): number {
231+
const n = values.length;
232+
const vis: boolean[] = Array(n).fill(false);
233+
let ans = 0;
234+
let i = 0;
235+
236+
while (i >= 0 && i < n && !vis[i]) {
237+
vis[i] = true;
238+
if (instructions[i][0] === 'a') {
239+
ans += values[i];
240+
i += 1;
241+
} else {
242+
i += values[i];
243+
}
244+
}
245+
246+
return ans;
247+
}
248+
```
249+
250+
<!-- tabs:end -->
251+
252+
<!-- solution:end -->
253+
254+
<!-- problem:end -->

0 commit comments

Comments
 (0)