Published:
Updated:

  • Reference
  • left_sum, right_sum 배열을 λ§Œλ“€μ§€ 말고 λ°”λ‘œλ°”λ‘œ μ²˜λ¦¬ν•˜λ©΄ 더 μ’‹μœΌλ €λ‚˜?
  • λ‹€λ₯Έ μ‚¬λžŒλ“€ 풀이 λ³΄λ‹ˆ λ‚˜μ²˜λŸΌ ν‘Ό μ‚¬λžŒμ€ 단 ν•œλͺ…도 μ—†μ—ˆλ‹€..
    • λ‚΄κ°€ 생각해도 μ’€ λ³΅μž‘ν•œ 풀이 γ… γ… 


  • Runtime 71ms
  • Beats 83.96%


Solution

from typing import List


class Solution:
    # nums: [10, 4, 8, 3]
    # left: [0, 0 + 10, 0 + 10 + 4, 0 + 10 + 4 + 8] = [0, 10, 14, 22] -> 4번만 ν•˜κ³  쀑단
    # right: [0 + 3 + 8 + 4, 0 + 3 + 8, 0 + 3, 0] = [15, 11, 3, 0] -> 4번만 ν•˜κ³  쀑단
    def leftRigthDifference(self, nums: List[int]) -> List[int]:
        left_sum = [0] * len(nums)
        right_sum = [0] * len(nums)
        left = 0
        right = 0

        for i in range(1, len(nums)):
            left += nums[i - 1]
            left_sum[i] = left

        for i in range(len(nums) - 2, -1, -1):
            right += nums[i + 1]
            right_sum[i] = right

        # λ°°μ—΄ μ›μ†Œλ³„λ‘œ λΉΌμ£Όκ³  μ ˆλŒ“κ°’
        return list(abs(x - y) for x, y in zip(left_sum, right_sum))


print(Solution().leftRigthDifference([10, 4, 8, 3]))

Leave a comment