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 = 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.
  • 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 < 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
Inputinstructions = ["jump","add","add","jump","add","jump"], values = [2,1,3,1,-2,-3]
Output1
The process visits indices 0, 2, 3, 4, and 5, accumulating 3 + (-2) = 1 before jumping back to already visited index 2.
Example 2
Inputinstructions = ["jump","add","add"], values = [3,1,1]
Output0
The 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

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate