3522. Calculate Score After Performing Instructions LeetCode Solution

In this guide, you will get 3522. Calculate Score After Performing Instructions LeetCode Solution with the best time and space complexity. The solution to Calculate Score After Performing Instructions problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Calculate Score After Performing Instructions solution in C++
  4. Calculate Score After Performing Instructions solution in Java
  5. Calculate Score After Performing Instructions solution in Python
  6. Additional Resources
3522. Calculate Score After Performing Instructions LeetCode Solution image

Problem Statement of 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 (i.e., 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:

Input: instructions = [“jump”,”add”,”add”,”jump”,”add”,”jump”], values = [2,1,3,1,-2,-3]
Output: 1
Explanation:
Simulate the process starting at instruction 0:

At index 0: Instruction is “jump”, move to index 0 + 2 = 2.
At index 2: Instruction is “add”, add values[2] = 3 to your score and move to index 3. Your score becomes 3.
At index 3: Instruction is “jump”, move to index 3 + 1 = 4.
At index 4: Instruction is “add”, add values[4] = -2 to your score and move to index 5. Your score becomes 1.
At index 5: Instruction is “jump”, move to index 5 + (-3) = 2.
At index 2: Already visited. The process ends.

Example 2:

Input: instructions = [“jump”,”add”,”add”], values = [3,1,1]
Output: 0
Explanation:
Simulate the process starting at instruction 0:

At index 0: Instruction is “jump”, move to index 0 + 3 = 3.
At index 3: Out of bounds. The process ends.

Example 3:

Input: instructions = [“jump”], values = [0]
Output: 0
Explanation:
Simulate the process starting at instruction 0:

At index 0: Instruction is “jump”, move to index 0 + 0 = 0.
At index 0: Already visited. The process ends.

Constraints:

n == instructions.length == values.length
1 <= n <= 105
instructions[i] is either "add" or "jump".
-105 <= values[i] <= 105

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

3522. Calculate Score After Performing Instructions LeetCode Solution in C++

class Solution {
 public:
  long long calculateScore(vector<string>& instructions, vector<int>& values) {
    const int n = instructions.size();
    long ans = 0;
    int i = 0;
    vector<bool> seen(n);

    while (i >= 0 && i < n && !seen[i]) {
      seen[i] = true;
      if (instructions[i] == "add") {
        ans += values[i];
        ++i;
      } else if (instructions[i] == "jump") {
        i += values[i];
      }
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

3522. Calculate Score After Performing Instructions LeetCode Solution in Java

class Solution {
  public long calculateScore(String[] instructions, int[] values) {
    final int n = instructions.length;
    long ans = 0;
    int i = 0;
    boolean[] seen = new boolean[n];

    while (i >= 0 && i < n && !seen[i]) {
      seen[i] = true;
      if (instructions[i].equals("add")) {
        ans += values[i];
        ++i;
      } else if (instructions[i].equals("jump")) {
        i += values[i];
      }
    }

    return ans;
  }
}
// code provided by PROGIEZ

3522. Calculate Score After Performing Instructions LeetCode Solution in Python

class Solution:
  def calculateScore(self, instructions: list[str], values: list[int]) -> int:
    n = len(instructions)
    ans = 0
    i = 0
    seen = set()

    while 0 <= i < n and i not in seen:
      seen.add(i)
      if instructions[i] == 'add':
        ans += values[i]
        i += 1
      elif instructions[i] == 'jump':
        i += values[i]

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.