Skip to main content
Back to problems
#3522
Medium Algorithms

Calculate score after performing instructions

Array Hash Table String Simulation
56.9% acceptance
Feb 25, 2026
45
9
You are given two arrays, instructions and values, both of size n. Simulate the process: start at i=0 with score=0. "add": add values[i] to score, move to i+1. "jump": move to i+values[i]. Stop when out of bounds or revisiting an instruction. Return final score.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn calculate_score(instructions: Vec<String>, values: Vec<i32>) -> i64 {
    let n = instructions.len();
    let mut visited = vec![false; n];
    let mut score = 0i64;
    let mut i = 0i64;

    loop {
      if i < 0 || i >= n as i64 { break; }
      let idx = i as usize;
      if visited[idx] { break; }
      visited[idx] = true;

      if instructions[idx] == "add" {
        score += values[idx] as i64;
        i += 1;
      } else {
        i += values[idx] as i64;
      }
    }

    score
  }
}