feat: 2025-10-28打卡
This commit is contained in:
36
134.gas-station.java
Normal file
36
134.gas-station.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* @lc app=leetcode id=134 lang=java
|
||||
*
|
||||
* [134] Gas Station
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
/**
|
||||
* 本题核心为:若从i开始,计算到j时发现不行,那么从(i, j]范围内任一站点开始也不行,易进行论证
|
||||
*
|
||||
* @param gas
|
||||
* @param cost
|
||||
* @return
|
||||
*/
|
||||
public int canCompleteCircuit(int[] gas, int[] cost) {
|
||||
int start = 0, sum = 0, curIdx = 0;
|
||||
while(start < gas.length) {
|
||||
if(curIdx - start == gas.length) {
|
||||
return start;
|
||||
}
|
||||
int pos = curIdx % gas.length;
|
||||
if(sum + gas[pos] - cost[pos] < 0) {
|
||||
curIdx++;
|
||||
start = curIdx;
|
||||
sum = 0;
|
||||
} else {
|
||||
sum += gas[pos] - cost[pos];
|
||||
curIdx++;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
Reference in New Issue
Block a user