[LeetCode_724] Find Pivot Index(Python)

그냥·2024년 9월 19일
0

알고리즘

목록 보기
20/23

https://leetcode.com/problems/find-pivot-index/description/?envType=study-plan-v2&envId=leetcode-75

문제


코드

class Solution:
    def pivotIndex(self, nums: List[int]) -> int:
        total = sum(nums)
        nums = [0] + nums
        for i in range(1, len(nums)):
            nums[i] += nums[i-1]
        for i in range(1, len(nums)):
            if (nums[i] + nums[i-1]) == total:
                return i - 1
        return -1
        

Idea

  • 누적합 리스트 계산
  • 리스트 전체 합 == (현재 인덱스 까지 누적합 + 이전 인덱스 까지 누적합) return

0개의 댓글