题目来源:

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


题意分析:

  在一个圈子路线里面有N个汽油站,i站的汽油有gas[i]汽油。现在有一辆无限容量的车,它从i站开到(i+1)需要耗费cost[i]汽油。如果这辆车可以走完这个圈,那么返回这个车的起点,否者返回-1.


题目思路:

  不难发现,如果gas的总和大于或等于cost的总和,必然存在一种路线使得走完整个圈子。那么只要找到一个起点i,从这个起点出发的所有gas的和总比cost的和大就可以了。


代码(python):

class Solution(object):
def canCompleteCircuit(self, gas, cost):
"""
:type gas: List[int]
:type cost: List[int]
:rtype: int
"""
begin,subsum,sum,i = 0,0,0,0
while i < len(gas):
sum += gas[i] - cost[i]
subsum += gas[i] - cost[i]
if subsum < 0:
subsum,begin = 0,i + 1
i += 1
if sum < 0:
return -1
else:
return begin

[LeetCode]题解(python):134-Gas Station的更多相关文章

  1. 134. Gas Station leetcode

    134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...

  2. 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters

    870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  3. leetcode@ [134] Gas station (Dynamic Programming)

    https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...

  4. [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 ...

  5. 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 ...

  6. leetcode 134. Gas Station ----- java

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

  7. 【LeetCode】134.Gas Station

    Problem: There are N gas stations along a circular route, where the amount of gas at station i is ga ...

  8. [leetcode greedy]134. Gas Station

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

  9. [leetcode]134. Gas Station加油站

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

  10. Java for 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 ...

随机推荐

  1. 【转】python import的用法

    [转自http://blog.sina.com.cn/s/blog_4b5039210100ennq.html] 在python用import或者from...import来导入相应的模块.模块其实就 ...

  2. saiku安装方法总结

    最近研究pentaho和saiku,在网上搜集了一些安装和配置的方法,亲测有效,在这分享总结一下方便日后使用. Saiku主要提供两种安装方式,独立运行和集成在Pentaho BI平台上,本文会简单介 ...

  3. Codeforces Round #277.5 (Div. 2)-D

    题意:求该死的菱形数目.直接枚举两端的点.平均意义每一个点连接20条边,用邻接表暴力计算中间节点数目,那么中间节点任选两个与两端可组成的菱形数目有r*(r-1)/2. 代码: #include< ...

  4. js实现页面时钟

    <body onload="setInterval(nowtime,1000)">    <form id="main" method=&qu ...

  5. Spring MVC返回的json如何去除根节点名称

    spring xml中配置视图如果是如下 <property name="defaultViews"> <list> <bean class=&quo ...

  6. System.Web.HttpException: 无法向会话状态服务器发出会话状态请求

    System.Web.HttpException: 无法向会话状态服务器发出会话状态请求.请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器 ...

  7. FORM表单不刷新提交POST数据

    很多时候表单太多项,JQ懒的去处理了 使用这个提交吧.和她讨论出去旅游,去哪里好呢,此时还和以前一样吗? function testaction(){ var f = $("#publish ...

  8. fiddler--firefiox代理

    修改端口:修改后重启才能生效

  9. JAVA泛型-自动包装机制不能应用于泛型数据的测试

    <thinging in java>中指出自动包装机制不能应用于泛型数据,自己写的测试代码如下: package com.xt.thinks15_11_1; import java.uti ...

  10. Storm博客收集

    http://wbj0110.iteye.com/category/292875 http://blog.csdn.net/hguisu/article/details/8454368?reload ...