[leetcode]Gas Station @ Python
原题地址:https://oj.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 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.
Note:
The solution is guaranteed to be unique.
解题思路:这道题也很tricky,自己想是很难想出来的。如果sum(gas)<sum(cost)的话,那么一定无解。diff是走完一站邮箱剩下的油,如果加上gas[i]也到不了下一站,那么继续将下一站设置为起点,然后再检查,是不是很巧妙呢?
代码:
class Solution:
# @param gas, a list of integers
# @param cost, a list of integers
# @return an integer
def canCompleteCircuit(self, gas, cost):
if sum(gas) < sum(cost): return -1
n = len(gas)
diff = 0
stationIndex = 0
for i in range(n):
if gas[i]+diff < cost[i]: stationIndex = i+1; diff = 0
else: diff += gas[i]-cost[i]
return stationIndex
[leetcode]Gas Station @ Python的更多相关文章
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- [LeetCode] Gas Station
Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...
- LeetCode: Gas Station 解题报告
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
- [LeetCode] Gas Station,转化为求最大序列的解法,和更简单简单的Jump解法。
LeetCode上 Gas Station是比较经典的一题,它的魅力在于算法足够优秀的情况下,代码可以简化到非常简洁的程度. 原题如下 Gas Station There are N gas stat ...
- LeetCode——Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- [Leetcode] gas station 气站
There are N gas stations along a circular route, where the amount of gas at station i isgas[i]. You ...
- [LeetCode] 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 解题思路
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 ...
随机推荐
- ADSL
ADSL属于DSL技术的一种,全称Asymmetric Digital Subscriber Line( 非对称数字用户线路),亦可称作非对称数字用户环路.是一种新的数据传输方式. ADSL技术提供的 ...
- JSP的几个页面指令
页面指令:向服务器说明页面自身的特征,以便服务器. 1,<%@page contentType="text/xml;charset=utf-8" %> 客户端---&g ...
- 洛谷 P1135 奇怪的电梯
题目描述 呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯.大楼的每一层楼都可以停电梯,而且第i层楼(1<=i<=N)上有一个数字Ki(0<=Ki<=N).电梯只有四个按钮:开 ...
- hihoCoder.1457.后缀自动机四 重复旋律7(广义后缀自动机)
题目链接 假设我们知道一个节点表示的子串的和sum,表示的串的个数cnt,那么它会给向数字x转移的节点p贡献 \(sum\times 10+c\times cnt\) 的和. 建广义SAM,按拓扑序正 ...
- clientHeight ,offsetHeight,style.height,scrollHeight有区别与联系
style.height 包括 元素的滚动条,不包括边框clientHeight 不包括元素的滚动条和边框,只有在没有元素的滚动条的情况下,style.height相等于clientHeightoff ...
- javascript区域打印代码
这段代码是我从Highcharts的代码中改造出来的,非常感谢Highcharts的作者,先链上Highcharts的地址http://www.highcharts.com/,(Highcharts的 ...
- 【转】topcoder插件配置(傻瓜教程-图文版)
地址:http://whucc2009luochen.blog.163.com/blog/static/1305715602010827102342860/ 1.插件下载地址:http://www.t ...
- [Deepin 15] 编译安装 PHP-5.6.30
先看下历史笔记: Ubuntu 14 编译安装 PHP 5.4.45 + Nginx 1.4.7 + MySQL 5.6.26 笔记 ################################# ...
- [Go] 命令行参数解析包(flag 包)使用详解
Go 的 flag 包可以解析命令行的参数. 一.命令行语法 命令行语法主要有以下几种形式: cmd -flag // 只支持bool类型 cmd -flag=xxx cmd -flag ...
- Win10年度更新开发必备:VS2015 Update 2正式版下载汇总
========================================================================== 微软在03月30日发布了Visual Studio ...