Tech for good

[Leetcode/Sliding Window] 1652. Defuse the Bomb ๋ณธ๋ฌธ

IT/Computer Science

[Leetcode/Sliding Window] 1652. Defuse the Bomb

Diana Kang 2025. 3. 3. 01:24

๐Ÿ” ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ(Sliding Window)

  • ๋ฐฐ์—ด์—์„œ ๋ถ€๋ถ„ ํ•ฉ์„ ํšจ์œจ์ ์œผ๋กœ ๊ณ„์‚ฐํ•˜๋Š” ๊ธฐ๋ฒ•
  • ๋งค๋ฒˆ ์ „์ฒด ํ•ฉ์„ ๋‹ค์‹œ ๊ณ„์‚ฐํ•˜์ง€ ์•Š๊ณ , ์ตœ์ดˆ ์œˆ๋„์šฐ์˜ ํ•ฉ์„ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•ด๋‘” ํ›„ ๊ธฐ์กด ์œˆ๋„์šฐ์˜ ๊ฐ’์„ ์กฐ๊ธˆ์”ฉ ๋ณ€๊ฒฝํ•˜๋ฉด์„œ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.
  • ์ฆ‰, ํ˜„์žฌ ํ•ฉ์—์„œ ๋น ์งˆ ๊ฐ’๊ณผ ์ถ”๊ฐ€ํ•  ๊ฐ’๋งŒ ์—…๋ฐ์ดํŠธํ•˜๋ฉด O(1) ์‹œ๊ฐ„์— ๊ฐฑ์‹ ์ด ๊ฐ€๋Šฅํ•œ ํšจ์œจ์  ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค! 

class Solution:
    def decrypt(self, code: List[int], k: int) -> List[int]:
        n = len(code)  # ์›๋ž˜ ์ฝ”๋“œ ๋ฐฐ์—ด์˜ ๊ธธ์ด
        
        # k๊ฐ€ 0์ด๋ฉด ๋ชจ๋“  ์›์†Œ๋ฅผ 0์œผ๋กœ ๋ณ€๊ฒฝ
        if k == 0:
            return [0] * n
        
        # Extend code to handle circular array easily
        code_extended = code * 2  # ์˜ˆ: [2,4,9,3] -> [2,4,9,3,2,4,9,3]
        
        result = [0] * n  # ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด

        # ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ์˜ ์‹œ์ž‘์ ๊ณผ ๋์  ๊ฒฐ์ •
        # k > 0: ํ˜„์žฌ ์›์†Œ์˜ ๋‹ค์Œ k๊ฐœ์˜ ํ•ฉ์„ ๊ตฌํ•ด์•ผ ํ•จ
        # k < 0: ํ˜„์žฌ ์›์†Œ์˜ ์ด์ „ k๊ฐœ์˜ ํ•ฉ์„ ๊ตฌํ•ด์•ผ ํ•จ
        start, end = (1, k) if k > 0 else (n + k, n - 1)

        # Initial sum of the first window
        window_sum = sum(code_extended[start:end + 1])

        # ์›๋ž˜ ๋ฐฐ์—ด์˜ ๊ฐ ์›์†Œ์— ๋Œ€ํ•ด ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์ ์šฉ
        for i in range(n):
            result[i] = window_sum  # ํ˜„์žฌ ์œˆ๋„์šฐ ํ•ฉ์„ ๊ฒฐ๊ณผ ๋ฐฐ์—ด์— ์ €์žฅ
            
            # Slide the window forward
            window_sum -= code_extended[start]  # ํ˜„์žฌ ์œˆ๋„์šฐ์—์„œ ๊ฐ€์žฅ ์™ผ์ชฝ ์›์†Œ ์ œ๊ฑฐ
            window_sum += code_extended[end + 1]  # ์ƒˆ๋กญ๊ฒŒ ํฌํ•จ๋  ์›์†Œ ์ถ”๊ฐ€

            start += 1
            end += 1

        return result
  • start, end = (1, k) if k > 0 else (n + k, n - 1):
    • k > 0์ด๋ฉด ํ˜„์žฌ ์›์†Œ์˜ ๋‹ค์Œ k๊ฐœ๋ฅผ ๋”ํ•ด์•ผ ํ•˜๋ฏ€๋กœ start = 1, end = k.
      • start = 1: ํ˜„์žฌ ์ธ๋ฑ์Šค i์—์„œ 1์นธ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ํ•œ ๊ฐ’๋ถ€ํ„ฐ ํ•ฉ์‚ฐ ์‹œ์ž‘
      • end = k: ํ˜„์žฌ ์ธ๋ฑ์Šค i์—์„œ ์˜ค๋ฅธ์ชฝ k๋ฒˆ์งธ๊นŒ์ง€ ํฌํ•จ
      • ์ฆ‰, i์˜ ์˜ค๋ฅธ์ชฝ k๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋กœ ํ•ฉ์‚ฐ
    • k < 0์ด๋ฉด ํ˜„์žฌ ์›์†Œ์˜ ์ด์ „ k๊ฐœ๋ฅผ ๋”ํ•ด์•ผ ํ•˜๋ฏ€๋กœ start = n + k, end = n - 1.
      • ํ˜„์žฌ ์ˆซ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ ๋ฐฉํ–ฅ์œผ๋กœ |k|๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋”ํ•ด์•ผ ํ•จ
      • start = n + k:
        • ์˜ˆ๋ฅผ ๋“ค์–ด, code = [2, 4, 9, 3], k = -2 ----->  n = 4, k = -2์ด๋ฉด start = 4 - 2 = 2
        • ์ฆ‰, ํ˜„์žฌ ์ธ๋ฑ์Šค i์—์„œ ์™ผ์ชฝ |k|๊ฐœ ์ „ ์œ„์น˜์˜ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์‹œ์ž‘
      • end = n - 1:
        • n = 4์ด๋ฉด end = 3
        • ์ฆ‰, ํ˜„์žฌ ์›์†Œ ๋ฐ”๋กœ ์™ผ์ชฝ(์ด์ „ ์›์†Œ)๊นŒ์ง€ ํฌํ•จ
      • ์ฆ‰, i์˜ ์™ผ์ชฝ |k|๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋กœ ํ•ฉ์‚ฐ
  • window_sum = sum(code_extended[start:end + 1]):
    • ์ฒซ ๋ฒˆ์งธ ์›์†Œ์— ๋Œ€ํ•ด k๊ฐœ์˜ ํ•ฉ์„ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐ.

๐Ÿ“Œ ์˜ˆ์ œ ์ ์šฉ

1๏ธโƒฃ code = [5,7,1,4], k = 3

  • start = 1, end = 3
  • window_sum = sum(code_extended[1:4]) → ๋‹ค์Œ 3๊ฐœ์˜ ์ˆซ์ž ํ•ฉ

2๏ธโƒฃ code = [2,4,9,3], k = -2

  • start = 4 - 2 = 2, end = 4 - 1 = 3
  • window_sum = sum(code_extended[2:4]) → ์ด์ „ 2๊ฐœ์˜ ์ˆซ์ž ํ•ฉ

โณ ์‹œ๊ฐ„ ๋ณต์žก๋„ ๋ถ„์„

  • ์ดˆ๊ธฐ window_sum ๊ณ„์‚ฐ: O(k)
  • ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์—…๋ฐ์ดํŠธ: O(n)
  • ์ด ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)