134 Gas Station

# 134 Gas Station

题目来源:

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

题意分析:

N个气站围成了圈,每个气站有gas[i],你有一辆车可以从任一气站出发,从气站i出发到i+1需要消耗气cost[i],问能否从任一气站出发,保证车中途气不用完且能回到起点。可以的话输出起点坐标,否则输出-1.

题目思路:

显而易见的是如果sum(gas)<sum(cost),是不可能走完的。

最简单的想法,就是以每个气站为起点,遍历一次,找到那个唯一解。时间复杂度为O(n^2)n为气站数目。

然后我们发现一个定理:

如果我们从第i个气站出发,第一个不能到达的气站Indexj,那么从第i+1..j-1个气站出发也不能到达第j个气站。

证明如下:

以从第i+1气站出发为例,从第i气站出发到达第i+1气站后剩余的气>=0,再从i+1出发,仍然到达不了第j气站。从第i+1气站出发的话开始剩余的气为0,比上述的情况要糟糕,所以也到达不了第j气站。i+2..j-1个气站出发同理,第i气站到达出发点时剩余的气>=0

所以我们可以实现气站的跳跃,从0气站出发,一旦发现到达j+1气站后剩余的气<0(sum+gas[j]-cost[j]<0),即不能到达j+1气站,就从j+1气站重新出发。因为题目保证具有唯一解,所以只需要记录最后的出发点k即可。k前面的点肯定不行,k后面的点如果是解,那k可以到达后面的点,那么k也是解,与具有唯一解矛盾,所以该出发点为唯一解。

代码:

1. class Solution:  

2.     def canCompleteCircuit(self, gas, cost):  

3.         """ 

4.         :type gas: List[int] 

5.         :type cost: List[int] 

6.         :rtype: int 

7.         """  

8. #         diff=[0]*len(gas)  

9. #         for i in range(0,len(gas)):  

10. #             diff[i]=gas[i]-cost[i]  

11.           

12. #         for i in range(0,len(diff)):  

13. #             if (diff[i]>=0):  

14. #                 if (self.travel(diff,i)):  

15. #                     return i  

16. #         return -1  

17.                   

18. #     def travel(self,diff,pos):  

19. #         left=0  

20. #         for i in range(0,len(diff)):  

21. #             left=left+diff[(i+pos)%len(diff)]  

22. #             if (left<0):  

23. #                 return False  

24. #         return True  

25.           

26.         sum=0  

27.         res=0  

28.         total=0  

29.         for i in range(len(cost)):  

30.             sum=sum+gas[i]-cost[i]  

31.             if (sum<0):  

32.                 total=total+sum  

33.                 sum=0  

34.                 res=i+1  

35.                   

36.         total=total+sum;  

37.         return -1 if total<0 else res  

 

提交细节:


  • 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、付费专栏及课程。

余额充值