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