Use limit thinking to break 134. Gas station - LeetCode algorithm

Topic: 134. Gas Station

There are n gas stations on a loop, and the i-th gas station has gas[i] liters of gasoline.
You have a car with unlimited fuel tank capacity, and it takes cost[i] liters of gasoline to drive from the i-th gas station to the i+1-th gas station. You start off from one of the gas stations, starting with an empty tank.
Given two integer arrays gas and cost, return the number of the gas station at the time of departure if you can travel around the circle, otherwise return -1. If a solution exists, it is guaranteed to be unique.

Example 1:
Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2]
Output: 3
Explanation:
Starting from gas station No. 3 (at index 3), Get 4 liters of petrol. At this time, the fuel tank has = 0 + 4 = 4 liters of gasoline
. Go to No. 4 gas station. At this time, the fuel tank has 4 - 1 + 5 = 8 liters of gasoline.
Go to No. 0 gas station, at this time, the fuel tank has 8 - 2 + 1 = 7 liters of gasoline Go
to No. 1 gas station, at this time the fuel tank has 7 - 3 + 2 = 6 liters of gasoline
Go to No. 2 gas station, at this time the fuel tank has 6 - 4 + 3 = 5 liters of gasoline
Go to No. 3 gas station, you It takes 5 liters of petrol, just enough to get you back to gas station #3.
Therefore, 3 can be the starting index.

Example 2:
Input: gas = [2,3,4], cost = [3,4,3]
Output: -1
Explanation:
You cannot start from gas station 0 or 1 because there is not enough gas for you Drive to the next gas station.
We start from gas station 2 and get 4 liters of petrol. At this time, the fuel tank has = 0 + 4 = 4 liters of gasoline.
Go to No. 0 gas station, and the fuel tank has 4 - 3 + 2 = 3 liters of gasoline.
Go to No. 1 gas station, and the fuel tank has 3 - 3 + 3 = 3 Liters of petrol
You can't go back to gas station 2 because the return journey takes 4 liters of petrol, but your tank only has 3 liters of petrol.
So, no matter what, you can't drive around the loop.

Link: https://leetcode-cn.com/problems/gas-station

Understanding and answer:

As for whether you can complete a lap, you can use the "extreme mode" way of thinking:
1. If the sum of the gas[] array of fuel gas[] is infinitely small, and the cost[] array of fuel consumption is infinitely large, then it is impossible to complete a lap. : (The car ran out of gas halfway, and couldn't go around in a circle, crying~);
2. If the gas[] array of refueling amount is infinitely large, and the cost[] array of fuel consumption is infinitely small, then you can definitely go Complete a circle (the car uses an infinite amount of fuel to walk an infinitely small circle, so easy~).

In fact, in many places where the algorithm is difficult to understand, you can understand it in seconds by using "extreme thinking".
insert image description here
Use limit thinking to understand.
insert image description here

class Solution {
    
    
    /**
       采用极限思维秒破 
     */
    public int canCompleteCircuit(int[] gas, int[] cost) {
    
    
        int gasSum = 0;
        int costSum = 0;
        
        for (int g = 0; g < gas.length; g++) {
    
    
            gasSum += gas[g];
        }
        for (int c = 0; c < cost.length; c++) {
    
    
            costSum += cost[c];
        }
        // 只要中间有一个无限大的路程,有限的油必然被耗光
        if (gasSum < costSum) {
    
    
            return -1;
        }

        // 上方不能走完圈,下方必然存在一个能走完圈的
        int restGas = 0; // 剩余油量
        int start = 0;
        for (int i = 0; i < gas.length; i++) {
    
    
            restGas = restGas + gas[i] - cost[i]; // 剩余油量+本站油量 - 下一段路程油耗
            if (restGas < 0) {
    
     // 没有办法到达下一站,那么就尝试将下一站作为起点,万一下一站有无限的油量呢~
                restGas = 0;
                start = i + 1; 
            }
        }
        return start;
    }
}
/**
    https://leetcode-cn.com/problems/gas-station/solution/tan-xin-dong-hua-tu-jie-dai-ma-jian-ji-b-na6b/
 */

Reference source:
https://leetcode-cn.com/problems/gas-station/solution/tan-xin-dong-hua-tu-jie-dai-ma-jian-ji-b-na6b/
https://xiaochen1024 .com/courseware/60b4f11ab1aa91002eb53b18/61963ce5c1553b002e57bf14

Guess you like

Origin blog.csdn.net/Xidian2850/article/details/123836764