Calculate Score After Performing Instructions
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 = 0with an initial score of0. - If
instructions[i]is"add": - Add
values[i]to your score. - Move to the next instruction,
i + 1. - If
instructions[i]is"jump": - Move to the instruction at index
i + values[i]without modifying your score.
The process ends when you either:
- Go out of bounds, meaning
i < 0ori >= 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
1The process visits indices 0, 2, 3, 4, and 5, accumulating 3 + (-2) = 1 before jumping back to already visited index 2.
Example 2
Input
instructions = ["jump","add","add"], values = [3,1,1]Output
0The first instruction jumps from index 0 to index 3, which is out of bounds, so the score remains 0.
Constraints
- n == instructions.length == values.length
- 1 <= n <= 10^5
- instructions[i] is either "add" or "jump".
- -10^5 <= values[i] <= 10^5