LeetCode "Gas Station" - 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/kenden23/article/details/14106137

Smart idea... I need practice more and think more....

class Solution {
public:
    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
        size_t cnt = gas.size();
        if (cnt == 1)
        {
            if (gas[0] >= cost[0]) return 0;
            return -1;
        }

        for (int i = 0; i < cnt; i++)
        {
            int delta = 0;
            int step;
            bool bFound = true;
            for (step = 0; step < cnt - 1; step++)
            {
                delta += gas[(i + step) % cnt] - cost[(i + step) % cnt];
                if (delta < 0)
                {
                    i += step;
                    bFound = false;
                    break;
                }
            }
            if (!bFound) continue;
            if (delta > 0) return i;
            else i += step - 1;
        }

        return -1;
    }
};

LeetCode "Gas Station" - TO BE REVISED

时间: 2024-10-09 05:36:58

LeetCode "Gas Station" - TO BE REVISED的相关文章

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 @ 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] 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 解题报告

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

[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 最大子序列和,贪心

在一个环形路径上分布着n个加油站,从一个加油站到下一个加油站会耗油.要找出一个起始点,从这个起始点出发,存油量>=耗油量.解保证唯一. 这要求在路径上不能出现非负的. 如果找出存油量最大的子序列,则可确保尽可能走完全程(贪心思想). 因此,需要在环形数组中找一个最大子序列和.最大子序列和是很经典的DP问题,而针对环形约束,采用的是将数组复制一份并放到原数组最后,然后采用普通的最大子序列求解.其间,需要记录起始位置(这是最终要求的),走过的站点数(站点数不能超过n,这是环形数组,长度为2n),最大