134. Gas Station | LeetCode | Top Interview 150 | 코딩 질문
발행: (2025년 12월 18일 오전 07:06 GMT+9)
1 min read
원문: Dev.to
Source: Dev.to
문제 링크
https://leetcode.com/problems/gas-station/

솔루션
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int totalGas = 0;
int totalCost = 0;
for (int i = 0; i < gas.length; i++) {
totalGas += gas[i];
totalCost += cost[i];
}
if (totalGas < totalCost) {
return -1;
}
int currGas = 0;
int startIndex = 0;
for (int i = 0; i < gas.length; i++) {
currGas += gas[i] - cost[i];
if (currGas < 0) {
startIndex = i + 1;
currGas = 0;
}
}
return startIndex;
}
}