Skip to main content
  1. LeetCode/

LeetCode 3522: Calculate Score After Performing Instructions

·1 min· ·
LeetCode Weekly Medium
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents
LeetCode Weekly Contest 446 - This article is part of a series.
Part 1: This Article

Meta Data
#

Difficulty: medium First Attempt: 2025-04-20

  • Total time: 11:48.00

Intuition
#

se a brute-force approach: walk through the instructions array and perform the corresponding action (add or jump).
If the step position goes out of bounds (either negative or greater than or equal to the length of the list), immediately stop the loop and return the score accumulated up to that point.
We also need to track visited steps using a seen set to prevent infinite loops.

Approach
#

class Solution:
    def calculateScore(self, instructions: List[str], values: List[int]) -> int:
        score = 0
        step = 0
        seen = set()
        n = len(instructions)
        
        while True:
            seen.add(step)
            if instructions[step] == "add":
                score += values[step]
                step+=1
                if step in seen:
                    break
            
            elif instructions[step] == "jump":
                step += values[step]
                if step in seen:
                    break
                    
            if step < 0 or step>= n:
                break
                
        return score

Findings
#

NA

Encountered Problems
#

NA

LeetCode Weekly Contest 446 - This article is part of a series.
Part 1: This Article

Related

LeetCode 3523: Make Array Non-decreasing
·1 min
LeetCode Weekly Medium
LeetCode Problem Solving
LeetCode 3517: Smallest Palindromic Rearrangement I
·1 min
LeetCode Weekly Medium
LeetCode Problem Solving
LeetCode 2381: Shifting Letters II
·2 mins
LeetCode Medium Difference Array
LeetCode Problem Solving
LeetCode 781: Rabbits in Forest
·2 mins
LeetCode Daily Medium
LeetCode Problem Solving
LeetCode 560: Subarray Sum Equals K
·2 mins
LeetCode Daily Medium Prefix Sum
LeetCode Problem Solving
LeetCode 1922: Count Good Numbers
·2 mins
LeetCode Daily Medium
LeetCode Problem Solving