Gas Station

从头往后扫描,时间O(n),空间O(1)。

从某个位置i开始计算,如果到某一位置j,累计的cost > gas,则以下一个位置i+1作为起始点,重新开始。起始点不可能是i和j中间的某个值。

反证法分析如下:参考

http://leetcodenotes.wordpress.com/2013/11/21/leetcode-gas-station-%E8%BD%AC%E5%9C%88%E7%9A%84%E5%8A%A0%E6%B2%B9%E7%AB%99%E7%9C%8B%E8%83%BD%E4%B8%8D%E8%83%BD%E8%B5%B0%E4%B8%80%E5%9C%88/

这个题要用反证法来理解。算法:

  1. 从i开始,j是当前station的指针,sum += gas[j] – cost[j] (从j站加了油,再算上从i开始走到j剩的油,走到j+1站还能剩下多少油)
  2. 如果sum < 0,说明从i开始是不行的。那能不能从i..j中间的某个位置开始呢?假设能从k (i <=k<=j)走,那么i..j < 0,若k..j >=0,说明i..k – 1更是<0,那从k处就早该断开了,根本轮不到j。
  3. 所以一旦sum<0,i就赋成j + 1,sum归零。
  4. 最后total表示能不能走一圈。

class Solution {
public:
    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
        
        int sum = 0;
        int total = 0;
        int j = 0;
        for(int i=0; i<gas.size(); i++)
        {
            sum += (gas[i]-cost[i]);
            total += (gas[i]-cost[i]);
            if(sum < 0)
            {
                j = i+1;
                sum = 0;
            }
        }
        return total>=0? j:-1;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用c++解决pipeline system consists of N transfer station, some of which are connected by pipelines. For each of M pipelines the numbers of stations A[i] and B[i], which are connected by this pipeline, and its profitability C[i] are known. A profitability of a pipeline is an amount of dollars, which will be daily yielded in taxes by transferring the gas through this pipeline. Each two stations are connected by not more than one pipeline. The system was built by Soviet engineers, who knew exactly, that the gas was transferred from Ukrainian gas fields to Siberia and not the reverse. That is why the pipelines are unidirectional, i.e. each pipeline allows gas transfer from the station number A[i] to the station number B[i] only. More over, if it is possible to transfer the gas from the station X to the station Y (perhaps, through some intermediate stations), then the reverse transfer from Y to X is impossible. It is known that the gas arrives to the starting station number S and should be dispatched to the buyers on the final station number F. The President ordered the Government to find a route (i.e. a linear sequence of stations which are connected by pipelines) to transfer the gas from the starting to the final station. A profitability of this route should be maximal. A profitability of a route is a total profitability of its pipelines. Unfortunately, the President did not consider that some pipelines ceased to exist long ago, and, as a result, the gas transfer between the starting and the final stations may appear to be impossible... Input The first line contains the integer numbers N (2 ≤ N ≤ 500) and M (0 ≤ M ≤ 124750). Each of the next M lines contains the integer numbers A[i], B[i] (1 ≤ A[i], B[i] ≤ N) and C[i] (1 ≤ C[i] ≤ 10000) for the corresponding pipeline. The last line contains the integer numbers S and F (1 ≤ S, F ≤ N; S ≠ F). Output If the desired route exists, you should output its profitability. Otherwise you should output "No solution".
05-28

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值