3522. Calculate Score After Performing Instructions
Description
You are given two arrays, instructions
and values
, both of size n
.
You need to simulate a process based on the following rules:
- You start at the first instruction at index
i = 0
with an initial score of 0. - If
instructions[i]
is"add"
:- Add
values[i]
to your score. - Move to the next instruction
(i + 1)
.
- Add
- If
instructions[i]
is"jump"
:- Move to the instruction at index
(i + values[i])
without modifying your score.
- Move to the instruction at index
The process ends when you either:
- Go out of bounds (i.e.,
i < 0 or i >= n
), or - Attempt to revisit an instruction that has been previously executed. The revisited instruction is not executed.
Return your score at the end of the process.
Example 1:
Input: instructions = ["jump","add","add","jump","add","jump"], values = [2,1,3,1,-2,-3]
Output: 1
Explanation:
Simulate the process starting at instruction 0:
- At index 0: Instruction is
"jump"
, move to index0 + 2 = 2
. - At index 2: Instruction is
"add"
, addvalues[2] = 3
to your score and move to index 3. Your score becomes 3. - At index 3: Instruction is
"jump"
, move to index3 + 1 = 4
. - At index 4: Instruction is
"add"
, addvalues[4] = -2
to your score and move to index 5. Your score becomes 1. - At index 5: Instruction is
"jump"
, move to index5 + (-3) = 2
. - At index 2: Already visited. The process ends.
Example 2:
Input: instructions = ["jump","add","add"], values = [3,1,1]
Output: 0
Explanation:
Simulate the process starting at instruction 0:
- At index 0: Instruction is
"jump"
, move to index0 + 3 = 3
. - At index 3: Out of bounds. The process ends.
Example 3:
Input: instructions = ["jump"], values = [0]
Output: 0
Explanation:
Simulate the process starting at instruction 0:
- At index 0: Instruction is
"jump"
, move to index0 + 0 = 0
. - At index 0: Already visited. The process ends.
Constraints:
n == instructions.length == values.length
1 <= n <= 105
instructions[i]
is either"add"
or"jump"
.-105 <= values[i] <= 105
Solutions
Solution 1: Simulation
We can simulate the process based on the problem description.
Define a boolean array $\textit{vis}$ of length $n$ to record whether each instruction has been executed. Initially, all elements are set to $\text{false}$.
Then, starting from index $i = 0$, perform the following steps in a loop:
Set $\textit{vis}[i]$ to $\text{true}$.
If the first character of $\textit{instructions}[i]$ is 'a', add $\textit{value}[i]$ to the answer and increment $i$ by $1$. Otherwise, increment $i$ by $\textit{value}[i]$.
The loop continues until $i \lt 0$, $i \ge n$, or $\textit{vis}[i]$ is $\text{true}$.
Finally, return the answer.
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{value}$.
Python3
class Solution:
def calculateScore(self, instructions: List[str], values: List[int]) -> int:
n = len(values)
vis = [False] * n
ans = i = 0
while 0 <= i < n and not vis[i]:
vis[i] = True
if instructions[i][0] == "a":
ans += values[i]
i += 1
else:
i = i + values[i]
return ans
Java
class Solution {
public long calculateScore(String[] instructions, int[] values) {
int n = values.length;
boolean[] vis = new boolean[n];
long ans = 0;
int i = 0;
while (i >= 0 && i < n && !vis[i]) {
vis[i] = true;
if (instructions[i].charAt(0) == 'a') {
ans += values[i];
i += 1;
} else {
i = i + values[i];
}
}
return ans;
}
}
C++
class Solution {
public:
long long calculateScore(vector<string>& instructions, vector<int>& values) {
int n = values.size();
vector<bool> vis(n, false);
long long ans = 0;
int i = 0;
while (i >= 0 && i < n && !vis[i]) {
vis[i] = true;
if (instructions[i][0] == 'a') {
ans += values[i];
i += 1;
} else {
i += values[i];
}
}
return ans;
}
};
Go
func calculateScore(instructions []string, values []int) (ans int64) {
n := len(values)
vis := make([]bool, n)
i := 0
for i >= 0 && i < n && !vis[i] {
vis[i] = true
if instructions[i][0] == 'a' {
ans += int64(values[i])
i += 1
} else {
i += values[i]
}
}
return
}
TypeScript
function calculateScore(instructions: string[], values: number[]): number {
const n = values.length;
const vis: boolean[] = Array(n).fill(false);
let ans = 0;
let i = 0;
while (i >= 0 && i < n && !vis[i]) {
vis[i] = true;
if (instructions[i][0] === 'a') {
ans += values[i];
i += 1;
} else {
i += values[i];
}
}
return ans;
}