[leetcode] 134 Gas Station

原创 2015年07月08日 09:49:40

题目:
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 empty tank at one of the gas stations.

Return the starting gas station’s index if you can travel around the circuit once, otherwise return -1.
题意:
在一个环路上有N个加油站,加油站i的油量是gas[i],从加油站i到它的下一个加油站需要用油cost[i],可以从任何一个加油站出发,找到开始的加油站的下标,可以保证能够绕圆环一圈。
思路:
从0号加油站开始,求和当前站加的油,并且减去接下来需要消耗的油,如果所有的求和相减小于0,那么即使一开始在某个加油站把所有的油全部加进车子也不够跑完全部。对于gas[i] - cost[i],如果是小于0,那么说明该站”入不敷出“,sum += gas[i] - cost[i],可以找到使sum最小的那个点的下标j,代表的就是从0号加油站到j号加油站,入不敷出之和最大,在接下来的j+1加油站,gas[j + 1] -cost[j + 1]一定是大于0,并且对于所有下标k > j,sum的值都比在i上的值大,也就是说gas[j + 1] - cost[j + 1] + … gas[k] - cost[k],的值一定大于等于0,也就是说对于这一段来说”入“肯定大于等于”支出“,所以在从j+1加油站之后都能够满足车厢里剩余的油加上在k加油站加的油gas[k],一定大于等于cost[k]。
所以只需要找到使sum最小的那个加油站j,从(j+1)%num_gas开始跑。
为什么不是j开始启动?因为,使sum最小的是j,那么代表在j-1时,sum并不是最小值,也就是说gas[j] - cost[j] < 0,所以不会从j开始启动。gas[j-1] - cost[j-1]可以大于0,只要gas[j-1] - cost[j-1] + gas[j] - cost[j] < 0,依旧说明sum在j时更小,所以说从j-1也不能启动,即使j-1上可以满足,但是到了j之后还是不能满足。依次类推。
代码如下:

class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        if(gas.size() == 0 || gas.size() != cost.size())return -1;
        int min = INT_MAX;
        int min_index = -1;
        int left = 0;
        for(auto i = 0; i < gas.size(); i++) {
            left += gas[i] - cost[i];
            if(left < min) {
                min = left;
                min_index = i;
            }
        }
        if(left < 0)return -1;
        else return (min_index + 1)%(gas.size());
    }
};

[leetcode] 【数组】134. Gas Station

There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...

【LeetCode】134. Gas Station

题目There are N gas stations along a circular route, where the amount of gas at station i is gas[i].Yo...

Leetcode——134. Gas Station && 435. Non-overlapping Intervals

Gas Station 1. 问题描述 有N个加油站围成一个圈,第i个加油站的油量是gas[i],一辆车有一个无限量的油箱,每次从第i个加油站开到第i+1个加油站需要消耗cost[i]的油量。车一开始...

leetcode134. Gas Station

134. Gas StationThere are N gas stations along a circular route, where the amount of gas at station ...

Leetcode-134-Gas Station C#

There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...

LeetCode134 Gas Station

详细见:leetcode.com/problems/gas-station Java Solution: github package leetcode; /* * There ...
  • zxwtry
  • zxwtry
  • 2017年05月17日 19:45
  • 148

[LeetCode]134.Gas Station

【题目】 There are N gas stations along a circular route, where the amount of gas at station i is  Th...

leetcode || 134、Gas Station

problem: There are N gas stations along a circular route, where the amount of gas at st...

LeetCode: Gas Station [134]

【题目】 There are N gas stations along a circular route, where the amount of gas at station i is gas[i...

134 Gas Station [Leetcode]

题目内容: There are N gas stations along a circular route, where the amount of gas at station i is gas...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode] 134 Gas Station
举报原因:
原因补充:

(最多只允许输入30个字)