본문 바로가기
python-algorithm

leetcode 2574. Left and Right Sum Differences

by 무적김두칠 2023. 2. 28.

https://leetcode.com/problems/left-and-right-sum-differences/description/

 

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    def leftRigthDifference(self, nums: List[int]) -> List[int]:
        nums.append(0)
        left_sum = 0
        right_sum = sum(nums) - nums[0]
 
        answer = []
 
        for i in range(len(nums)-1):
            answer.append(abs(left_sum-right_sum))
            left_sum+=nums[i]
            right_sum -= nums[i+1]
        return answer
cs

Line3에 for loop에 right_sum 을 뺄때 인덱스에러를 막기 위해서 0을 넣음
O(N)으로 해결 가능하네요

To avoid index error , In line3 I appended 0 into 'nums'
We can handle this problem with O(N)

반응형

'python-algorithm' 카테고리의 다른 글

leetcode 203. Remove Linked List Elements  (0) 2023.02.28
leetcode 1290. Convert Binary Number in a Linked List to Integer  (0) 2023.02.28
백준 13698 Hawk eyes  (0) 2023.02.27
백준 26575 Pups  (0) 2023.02.27
백준 26471 Farma  (0) 2023.02.24

댓글