본문 바로가기

Algorithm

[leetcode] 1413. Minimum Value to Get Positive Step by Step Sum _ python3

1413. Minimum Value to Get Positive Step by Step Sum

 

Minimum Value to Get Positive Step by Step Sum - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

풀이 코드

주어진 리스트를 탐색하며 running sum을 계산한다. 그리고 running sum이 최소인 값을 정답 변수에 저장한다. 최종 최소값이 1이상이면 1리턴, 1미만이면 절댓값+1을 리턴해준다.

class Solution:
    def minStartValue(self, nums: List[int]) -> int:
        answer = float('inf')
        amount = 0
        for i in nums:
            amount += i
            answer = min(answer,amount)
        return 1 if answer >= 1 else abs(answer) + 1