3522. Calculate Score After Performing Instructions Medium

@problem@discussion
#Array#Hash Table#String#Simulation



1/**
2 * [3522] Calculate Score After Performing Instructions
3 *
4 * You are given two arrays, instructions and values, both of size n.
5 * You need to simulate a process based on the following rules:
6 * 
7 * 	You start at the first instruction at index i = 0 with an initial score of 0.
8 * 	If instructions[i] is "add":
9 * 	
10 * 		Add values[i] to your score.
11 * 		Move to the next instruction (i + 1).
12 * 	
13 * 	
14 * 	If instructions[i] is "jump":
15 * 	
16 * 		Move to the instruction at index (i + values[i]) without modifying your score.
17 * 	
18 * 	
19 * 
20 * The process ends when you either:
21 * 
22 * 	Go out of bounds (i.e., i < 0 or i >= n), or
23 * 	Attempt to revisit an instruction that has been previously executed. The revisited instruction is not executed.
24 * 
25 * Return your score at the end of the process.
26 *  
27 * <strong class="example">Example 1:
28 * <div class="example-block">
29 * Input: <span class="example-io">instructions = ["jump","add","add","jump","add","jump"], values = [2,1,3,1,-2,-3]</span>
30 * Output: <span class="example-io">1</span>
31 * Explanation:
32 * Simulate the process starting at instruction 0:
33 * 
34 * 	At index 0: Instruction is "jump", move to index 0 + 2 = 2.
35 * 	At index 2: Instruction is "add", add values[2] = 3 to your score and move to index 3. Your score becomes 3.
36 * 	At index 3: Instruction is "jump", move to index 3 + 1 = 4.
37 * 	At index 4: Instruction is "add", add values[4] = -2 to your score and move to index 5. Your score becomes 1.
38 * 	At index 5: Instruction is "jump", move to index 5 + (-3) = 2.
39 * 	At index 2: Already visited. The process ends.
40 * </div>
41 * <strong class="example">Example 2:
42 * <div class="example-block">
43 * Input: <span class="example-io">instructions = ["jump","add","add"], values = [3,1,1]</span>
44 * Output: <span class="example-io">0</span>
45 * Explanation:
46 * Simulate the process starting at instruction 0:
47 * 
48 * 	At index 0: Instruction is "jump", move to index 0 + 3 = 3.
49 * 	At index 3: Out of bounds. The process ends.
50 * </div>
51 * <strong class="example">Example 3:
52 * <div class="example-block">
53 * Input: <span class="example-io">instructions = ["jump"], values = [0]</span>
54 * Output: <span class="example-io">0</span>
55 * Explanation:
56 * Simulate the process starting at instruction 0:
57 * 
58 * 	At index 0: Instruction is "jump", move to index 0 + 0 = 0.
59 * 	At index 0: Already visited. The process ends.
60 * </div>
61 *  
62 * Constraints:
63 * 
64 * 	n == instructions.length == values.length
65 * 	1 <= n <= 10^5
66 * 	instructions[i] is either "add" or "jump".
67 * 	-10^5 <= values[i] <= 10^5
68 * 
69 */
70pub struct Solution {}
71
72// problem: https://leetcode.com/problems/calculate-score-after-performing-instructions/
73// discuss: https://leetcode.com/problems/calculate-score-after-performing-instructions/discuss/?currentPage=1&orderBy=most_votes&query=
74
75// submission codes start here
76
77impl Solution {
78    pub fn calculate_score(instructions: Vec<String>, values: Vec<i32>) -> i64 {
79        
80    }
81}
82
83// submission codes end
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn test_3522() {
91    }
92}
93

Back
© 2026 bowen.ge All Rights Reserved.