Input: instructions =["jump","add","add","jump","add","jump"], values =[2,1,3,1,-2,-3]Output: 1Explanation:
Simulate the process starting at instruction 0:* At index 0: Instruction is`"jump"`, move to index `0 + 2 = 2`.* At index 2: Instruction is`"add"`, add `values[2] = 3` to your score and move to index 3. Your score becomes 3.* At index 3: Instruction is`"jump"`, move to index `3 + 1 = 4`.* At index 4: Instruction is`"add"`, add `values[4] = -2` to your score and move to index 5. Your score becomes 1.* At index 5: Instruction is`"jump"`, move to index `5 + (-3) = 2`.* At index 2: Already visited. The process ends.
Input: instructions =["jump","add","add"], values =[3,1,1]Output: 0Explanation:
Simulate the process starting at instruction 0:* At index 0: Instruction is`"jump"`, move to index `0 + 3 = 3`.* At index 3: Out of bounds. The process ends.
Input: instructions =["jump"], values =[0]Output: 0Explanation:
Simulate the process starting at instruction 0:* At index 0: Instruction is`"jump"`, move to index `0 + 0 = 0`.* At index 0: Already visited. The process ends.
We simulate the process step by step, tracking visited instructions to avoid revisiting. For each instruction, we either add to the score or jump, and stop if we go out of bounds or revisit an instruction.
classSolution {
public:int calculateScore(vector<string>& instructions, vector<int>& values) {
int n = instructions.size(), ans =0, i =0;
unordered_set<int> vis;
while (i >=0&& i < n &&!vis.count(i)) {
vis.insert(i);
if (instructions[i] =="add") {
ans += values[i];
i++;
} else {
i += values[i];
}
}
return ans;
}
};
classSolution {
publicintcalculateScore(String[] instructions, int[] values) {
int n = instructions.length, ans = 0, i = 0;
Set<Integer> vis =new HashSet<>();
while (i >= 0 && i < n &&!vis.contains(i)) {
vis.add(i);
if (instructions[i].equals("add")) {
ans += values[i];
i++;
} else {
i += values[i];
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
funcalculateScore(instructions: Array<String>, values: IntArray): Int {
val n = instructions.size
var ans = 0var i = 0val vis = mutableSetOf<Int>()
while (i in0 until n && i !in vis) {
vis.add(i)
if (instructions[i] =="add") {
ans += values[i]
i++ } else {
i += values[i]
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution:
defcalculateScore(self, instructions: list[str], values: list[int]) -> int:
n, ans, i = len(instructions), 0, 0 vis = set()
while0<= i < n and i notin vis:
vis.add(i)
if instructions[i] =="add":
ans += values[i]
i +=1else:
i += values[i]
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use std::collections::HashSet;
impl Solution {
pubfncalculate_score(instructions: Vec<String>, values: Vec<i32>) -> i32 {
let n = instructions.len();
letmut ans =0;
letmut i =0i32;
letmut vis = HashSet::new();
while i >=0&& (i asusize) < n &&!vis.contains(&i) {
vis.insert(i);
if instructions[i asusize] =="add" {
ans += values[i asusize];
i +=1;
} else {
i += values[i asusize];
}
}
ans
}
}