Calculate Score After Performing Instructions - Problem
Instruction Execution Simulator
Imagine you're implementing a simple virtual machine that processes a sequence of instructions! ๐ฅ๏ธ
You are given two arrays:
The Rules:
โข Start at index
โข If
โข If
The simulation ends when:
โข You go out of bounds (
โข You attempt to revisit a previously executed instruction
Return the final score when the simulation terminates.
Imagine you're implementing a simple virtual machine that processes a sequence of instructions! ๐ฅ๏ธ
You are given two arrays:
instructions and values, both of size n. Your task is to simulate an instruction pointer moving through these arrays while maintaining a score.The Rules:
โข Start at index
i = 0 with score = 0โข If
instructions[i] == "add": Add values[i] to your score and move to i + 1โข If
instructions[i] == "jump": Move to index i + values[i] (no score change)The simulation ends when:
โข You go out of bounds (
i < 0 or i >= n), ORโข You attempt to revisit a previously executed instruction
Return the final score when the simulation terminates.
Input & Output
example_1.py โ Basic Simulation
$
Input:
instructions = ["add", "jump", "add", "jump", "add"], values = [5, 2, 3, -2, 1]
โบ
Output:
5
๐ก Note:
Start at i=0: execute 'add' with value 5, score=5, i=1. At i=1: execute 'jump' with value 2, i=3. At i=3: execute 'jump' with value -2, i=1. At i=1: already visited, stop. Return score=5.
example_2.py โ Out of Bounds
$
Input:
instructions = ["add", "add", "jump"], values = [1, 2, 10]
โบ
Output:
3
๐ก Note:
Start at i=0: execute 'add' with value 1, score=1, i=1. At i=1: execute 'add' with value 2, score=3, i=2. At i=2: execute 'jump' with value 10, i=12. i=12 is out of bounds, stop. Return score=3.
example_3.py โ Single Instruction
$
Input:
instructions = ["jump"], values = [1]
โบ
Output:
0
๐ก Note:
Start at i=0: execute 'jump' with value 1, i=1. i=1 is out of bounds, stop. Return score=0 (no adds were executed).
Visualization
Tap to expand
Understanding the Visualization
1
Initialize
Set up instruction pointer at 0, score at 0, and empty visited set
2
Check Bounds
Verify instruction pointer is within valid range
3
Detect Cycle
Check if current position was visited before using HashSet
4
Execute
Process the instruction: add to score or jump to new position
5
Update State
Mark current position as visited and update pointer
6
Repeat
Continue until out of bounds or cycle detected
Key Takeaway
๐ฏ Key Insight: Use a HashSet for O(1) cycle detection during instruction simulation - this prevents infinite loops while maintaining optimal performance!
Time & Space Complexity
Time Complexity
O(n)
Each instruction is visited at most once, O(1) set operations
โ Linear Growth
Space Complexity
O(n)
HashSet stores at most n visited indices
โก Linearithmic Space
Constraints
- 1 โค n โค 1000
-
instructions[i] is either
"add"or"jump" - -1000 โค values[i] โค 1000
- The arrays instructions and values have the same length
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code