로딩
요청 처리 중입니다...

JAVA_LeetCode 134_Gas Station

 JAVA_LeetCode 134_Gas Station

JAVA_LeetCode 134_Gas Station 풀이 class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { // 한바퀴 돌 수 있는 지점을 찾기 int totalGas = 0, totalCost = 0, tank = 0, start = 0; // 누적 연료가 음수가 되는 순간, 그 구간에서 출발해 봐야 도달 못함. for (int i = 0; i < gas.length; i++) { totalGas += gas[i]; totalCost += cost[i]; tank += gas[i] - cost[i]; // 다음 인덱스(i + 1)부터 다시 도전. if(tank < 0){ start = i + 1; tank = 0; } } // 전체 얻는 연료 < 전체 필요한 연료라면 불가능 if(totalGas < totalCost) return -1; return start; } } 선형 탐색 풀이, 한바퀴 돌 때 ...