diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index ca95af67..45e05fed 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -341,6 +341,7 @@ var canCompleteCircuit = function(gas, cost) { ``` ### C +贪心算法:方法一 ```c int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){ int curSum = 0; @@ -370,5 +371,36 @@ int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){ } ``` +贪心算法:方法二 +```c +int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){ + int curSum = 0; + int totalSum = 0; + int start = 0; + + int i; + for(i = 0; i < gasSize; ++i) { + // 当前i站中加油量与耗油量的差 + int diff = gas[i] - cost[i]; + + curSum += diff; + totalSum += diff; + + // 若0到i的加油量都为负,则开始位置应为i+1 + if(curSum < 0) { + curSum = 0; + // 当i + 1 == gasSize时,totalSum < 0(此时i为gasSize - 1),油车不可能返回原点 + start = i + 1; + } + } + + // 若总和小于0,加油车无论如何都无法返回原点。返回-1 + if(totalSum < 0) + return -1; + + return start; +} +``` + -----------------------