Simulate the instructions in order and compute the total score earned before the process stops.
Problem
You are given an array of instructions. Start from the first instruction and keep moving forward while accumulating a score.
For each step, the current instruction tells you how far to jump next and how much score to add. However, if you ever move to an index you have already visited, the process stops immediately and you do not execute that instruction again.
Return the total score collected before the process terminates.
Notes
- The instructions are processed sequentially by jumping from one index to another.
- An index can be visited at most once.
- As soon as a repeated index would be executed, stop and return the score so far.
Input Format
- An integer array
instructions. - Each element describes the jump/score behavior for that position.
Output Format
- Return an integer representing the final accumulated score before a repeated index is encountered or the walk can no longer continue.
Constraints
1 <= instructions.length- The exact numeric bounds are not provided in the source metadata.
- The intended solution should run in linear time or close to it.
- Use a set or hash map to detect repeated visits efficiently.
Example 1
Input
instructions = [2, 3, 1, 2, 4]
Output
9
Explanation
Illustrative example based on the instruction-walk format; actual platform details may differ.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.