[LeetCode]题解(python):134-Gas Station

题目来源:

  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

时间: 2024-10-19 14:06:13

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

[Lintcode]187. Gas Station/[Leetcode]134. Gas Station

187. Gas Station/134. Gas Station 本题难度: Medium Topic: Greedy Description 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

134. 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 i to its next station (i+1). You begin the

134. Gas Station(js)

134. Gas Station There are N gas stations along a circular route, where the amount of gas at station iis 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 j

[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 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]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 i to its next station (i+1). You begin the journey with an e

[email&#160;protected] [134] Gas station (Dynamic Programming)

https://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 its next sta

134. Gas Station (Array; DP)

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

[leedcode 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 i to its next station (i+1). You begin the journey with an e

134 Gas Station 加油站

在一条环路上有 N 个加油站,其中第 i 个加油站有汽油gas[i].你有一辆油箱容量无限的的汽车,从第 i 个加油站前往第 i+1 个加油站需要消耗汽油 cost[i].你从其中一个加油站出发,开始时油箱为空.如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回-1.注意:给定的数据可保证答案是唯一的.详见:https://leetcode.com/problems/gas-station/description/ class Solution { public: int canCom

[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

全部最新的题解可以在 我的 github 上找,欢迎 star 和 watch ~ 更新中~~ 说明 这个系列的题解包括用 C++/Java/Python 写的 leetcode 上的算法题目,和 Sql 写的 leetcode 上的数据库题目. 有些题目虽然 AC 了却还没写分析,所以这次就开坑来完成. 链接: 我的 github Leetcode Algorithms Problems Leetcode Database Problems CSDN 题解索引 001.Two_Sum (Med