[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 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个加油站,每个加油站有gap[i]的汽油,从加油站i到i+1耗油为cost[i],问一个油箱为无限大的汽车,能不能走完全程,能的话起点在哪
思路:如果环路中gas的总量比cost的总量多,那么肯定有一种方法可以让汽车跑完全程,首先什么情况下汽车不能跑完全程?比如从i出发到i+n时候,邮箱变为负值了,那么就可以把从i到i+n的节点全部滤掉
做法和求最大子列和一样
class Solution(object):
def canCompleteCircuit(self, gas, cost):
if not len(gas) or not len(cost) or sum(gas)<sum(cost):
return -1
balance,p= 0,0
for i in range(len(gas)):
balance += gas[i]-cost[i]
if balance < 0:
balance = 0
p = i+1
return p
[leetcode greedy]134. Gas Station的更多相关文章
- 【LeetCode】134.Gas Station
Problem: There are N gas stations along a circular route, where the amount of gas at station i is ga ...
- 134. Gas Station leetcode
134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^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的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- [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 ...
- leetcode@ [134] Gas station (Dynamic Programming)
https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- slice层解析
如果说之前的Concat是将多个bottom合并成一个top的话,那么这篇博客的slice层则完全相反,是把一个bottom分解成多个top,这带来了一个问题,为什么要这么做呢?为什么要把一个低层的切 ...
- sql server 查询本年的每个月的数据
一.以一行数据的形式,显示本年的12月的数据,本示例以2017年为例,根据CreateDate字段判断,计算总和,查询语句如下: end) as '1月', end) as '2月', end) as ...
- linux命令-grep+正则表达式用法
目标文件/etc/passwd,使用grep命令或egrep 1.显示出所有含有root的行:egrep 'root' passwd 2.输出任何包含bash的所有行,还要输出紧接着这行的上下各两行的 ...
- 【译】第五篇 Integration Services:增量加载-Deleting Rows
本篇文章是Integration Services系列的第五篇,详细内容请参考原文. 在上一篇你学习了如何将更新从源传送到目标.你同样学习了使用基于集合的更新优化这项功能.回顾增量加载记住,在SSIS ...
- Java 8 Lambda表达式,让你的代码更简洁
Lambda表达式是Java 8一个非常重要的新特性.它像方法一样,利用很简单的语法来定义参数列表和方法体.目前Lambda表达式已经成为高级编程语言的标配,像Python,Swift等都已经支持La ...
- npm_一个有意思的npm包
$ npm install yosay const yosay = require('yosay'); console.log(yosay('Hello, and welcome to my fant ...
- Java IO,硬骨头也能变软
开胃菜 先看一张网上流传的http://java.io包的类结构图: 当你看到这幅图的时候,我相信,你跟我一样内心是崩溃的. 有些人不怕枯燥,不怕寂寞,硬着头皮看源码,但是,能坚持下去全部看完的又有几 ...
- DirectFB简介以及移植[一]【转】
转自:https://blog.csdn.net/wavemcu/article/details/39251805 ****************************************** ...
- [转载]Windows服务编写原理及探讨(3)
(三)对服务的深入讨论之下 现在我们还剩下一个函数可以在细节上讨论,那就是服务的CtrlHandler函数. 当调用RegisterServiceCtrlHandler函数时,SCM得到并保存这个回调 ...
- 说一下怎么搭建外网来访问SVN服务器
一.搭建SVN服务器 1.所需软件 TortoiseSVN,下载地址http://tortoisesvn.net/downloads.html TortoiseSVN中文语言包,下载地址http:// ...