Published:
Updated:

  • current_gas๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ๊ณ„์‚ฐํ•ด์ฃผ๋Š” ๋กœ์ง


Solution

from typing import List


class Solution:
    def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
        answer = 0

        if sum(gas) < sum(cost):
            return -1

        current_gas = 0
        for i in range(len(gas)):
            if gas[i] + current_gas < cost[i]:
                # ์ด if๋ฌธ์—์„œ๋Š” ํ˜„์žฌ ์œ„์น˜์—์„œ ์ถœ๋ฐœํ•  ์ˆ˜ ์—†๋‹ค๋Š” ์˜๋ฏธ
                # ์ฆ‰, answer๋ฅผ ๋‹ค์Œ index๋กœ ์—…๋ฐ์ดํŠธ ํ›„ tmp๋ฅผ ๋‹ค์‹œ 0์œผ๋กœ ์ดˆ๊ธฐํ™”
                answer = i + 1
                current_gas = 0
                continue

            current_gas += gas[i] - cost[i]

        return answer


Reference

Leave a comment