基本資料#
難易度: medium 第一次嘗試: 2025-04-20
- 總花費時間:11:48.00
解題思路#
採用暴力法:直接遍歷 instructions 陣列,根據指令進行對應動作(add 或 jump)。
當 step 的位置超出邊界(小於 0 或大於等於陣列長度)時,立即停止迴圈並回傳當前的累積分數。
同時,需要用 seen 集合記錄已經拜訪過的位置,避免無限迴圈的發生。
解法#
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
收穫#
NA
遇到的問題#
NA

