Leetcode 1755 Solution

This article provides solution to leetcode question 1755 (defuse-the-bomb)

https://leetcode.com/problems/defuse-the-bomb

Solution

class Solution:
    def decrypt(self, code: List[int], k: int) -> List[int]:
        prefix = 0
        prefix_arr = []

        for num in code + code:
            prefix += num
            prefix_arr.append(prefix)

        ans = []

        for i in range(len(code)):
            if k == 0:
                ans.append(0)
            elif k > 0:
                ans.append(prefix_arr[i + k] - prefix_arr[i])
            elif k < 0:
                ans.append(prefix_arr[len(code) + i - 1] - prefix_arr[len(code) + i - 1 + k])

        return ans