LeetCode-Gas Station-加油站-最大子串算法应用

https://oj.leetcode.com/problems/gas-station/

计算每个加油站的加油差diff[]。得到一个数组。从贪心的角度来说,如果我们找到一个最大子串,那么从他的起点l开始走,能够连续一直走并且累积最大量的汽油。

一个猜想是:如果这些汽油不足以走完全程,那么无论从哪里都无法走完全程。

可以反证一下:如果从p开始能够走完全程。那么从p走到l的这部分子串和必然大于0,这部分子串加上l开始最大子串能使子串更大。那么与从l开始最大子串矛盾。所以没有这样的p。

因此,用最大子串算法找出最大子串的起点l,然后执行一次模拟,就能确定能否走完全程了。

class Solution {
public:
    int n,m;
    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
        n=gas.size();
        vector<int> d=gas;
        for(int i=0;i<n;i++) d[i]-=cost[i];
        int l=-1;
        int maxs=-1;
        int cs=-1;
        int cl=-1;
        for(int i=0;i<2*n;i++) {
            int ci=i%n;
            if(cs<0) {cs=d[ci];cl=ci;}
            else cs+=d[ci];
            if(cs>maxs) {
                maxs=cs;
                l=cl;
            }
        }
        int cur=0;
        for (int i=0;i<n;i++) {
            int cp=(l+i)%n;
            cur+=d[cp];
            if(cur<0) return -1;
        }
        return l;
    }
};
时间: 2024-10-26 08:21:10

LeetCode-Gas Station-加油站-最大子串算法应用的相关文章

Leetcode:Gas Station 加油站

Gas Station There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journ

[LeetCode] Gas Station 加油站问题

There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an e

[LeetCode] Gas Station,转化为求最大序列的解法,和更简单简单的Jump解法。

LeetCode上 Gas Station是比较经典的一题,它的魅力在于算法足够优秀的情况下,代码可以简化到非常简洁的程度. 原题如下 Gas Station There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to

LeetCode——Gas Station

There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an e

LeetCode: Gas Station [134]

[题目] There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with

[leetcode]Gas Station @ Python

原题地址:https://oj.leetcode.com/problems/gas-station/ 题意: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to

LeetCode &quot;Gas Station&quot; - TO BE REVISED

First I thought of a DP solution.. but TLE. So there must be a O(n). I thought of Mono-Queue.. but looks too complex. So what is the naive solution? O(n^2) of course. If we pick next start station smartly, we can make it linear: http://blog.csdn.net/

[leetcode]134. Gas Station加油站

There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an e

LeetCode: Gas Station 解题报告

Gas Station There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journ

[LeetCode]Gas Station

新年啦,大家新年快乐~~ 由于新年呢,所以最近很少做题,今天终于有时间可以打打代码了 134. Gas Station. There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station