Gas Station——LeetCode
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.
题意大概是:有N个加油站围成一个圈,每个加油站的油量是gas[i],从第i个加油站开到第i+1个加油站需要耗费cost[i]油量,问是否有一个解使得从某个加油站开始跑一圈,有的话返回开始跑的加油站的index。
我的做法就是从0开始遍历,sum+=gas[i]-cost[i],一旦sum<0了,说明从当前到0的所有点都不可能了,然后从下一个开始,一旦跑完一圈发现sum都不小于0,那么就可以返回当前的index了,否则全部遍历一遍没有解就返回-1。
Talk is cheap>>
public int canCompleteCircuit(int[] gas, int[] cost) {
int res;
for (int i = 0; i < gas.length; i++) {
res = gas[i] - cost[i];
if (res >= 0) {
int pos = i;
for (int j = pos + 1; j < gas.length + pos; j++) {
int index = j % gas.length;
res += gas[index] - cost[index];
if (res < 0) {
pos = -1;
i = j;
break;
}
}
if (pos >= 0)
return pos;
}
}
return -1;
}
Gas Station——LeetCode的更多相关文章
- 134. Gas Station leetcode
134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...
- Gas Station [LeetCode]
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- Gas Station leetcode java
题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...
- Gas Station|leetcode 贪心
贪心:尽量将gas[i]-cost[i]>0的放在前面,gas[i]-cost[i]<0的放在后面.(路程的前面有汽油剩下,耗汽油的放在路程的后面). 能否全程通过的 条件 是:sum(g ...
- Gas Station [leetcode] 两个解决方案
因为gas的总数大于cost总时间.你将能够圈住整个城市. 第一溶液: 如果一開始有足够的油.从位置i出发.到位置k时剩余的油量为L(i,k). 对随意的k.L(i,k)依据i的不同,仅仅相差常数. ...
- [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@ [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 ...
随机推荐
- 第一篇:R语言数据可视化概述(基于ggplot2)
前言 ggplot2是R语言最为强大的作图软件包,强于其自成一派的数据可视化理念.当熟悉了ggplot2的基本套路后,数据可视化工作将变得非常轻松而有条理. 本文主要对ggplot2的可视化理念及开发 ...
- IOS内存等信息
1. 获取IOS APP占用的内存 #import <mach/mach.h> // ... void report_memory(void) { struct task_basic_in ...
- jquery在ie浏览器下中文乱码的问题
用jquery的ajax方法在调用后台数据发现中文乱码,无法解析中文的url,而在别的浏览器下面就不会,如下所示 $.ajax({ type:'get', url:'薛之谦-演员.lrc', asyn ...
- Android系统移植与驱动开发——第六章——使用实例来理解Linux驱动开发及心得
Linux驱动的工作方式就是交互.例如向Linux打印机驱动发送一个打印命令,可以直接使用C语言函数open打开设备文件,在使用C语言函数ioctl向该驱动的设备文件发送打印命令.编写Linux驱动最 ...
- 本文实例汇总了C#中@的用法,对C#程序设计来说有不错的借鉴价值。
具体如下: 一 字符串中的用法 1.学过C#的人都知道C# 中字符串常量可以以@ 开头声名,这样的优点是转义序列“不”被处理,按“原样”输出,即我们不需要对转义字符加上 \ (反斜扛),就可以轻松co ...
- Linux SSH: key, agent, keychain
以前遇到过一个问题,在用有些 Linux 发行版时,用 ssh-keygen 产生好了密钥对并上传到了目标服务器,但每次登录都要重新输入. 这与 ssh-agent 有关,看如下 man ssh-ag ...
- datediff(),dateadd(),datepart().datename()时间处理函数
原文链接:http://www.cnblogs.com/whz111/articles/2445777.html --convert(时间)对应数字表示的时间格式 select CONVERT(var ...
- 很棒的jQuery代码片段分享
jQuery实现的内链接平滑滚动 不需要使用太复杂的插件,只要使用下载这段代码即可实现基于内部链接的平滑滚动 $('a[href^="#"]').bind('click.smoot ...
- iOS视图控制对象生命周期-init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear、viewDidDisappear的区别及用途
init-初始化程序 viewDidLoad-加载视图 viewWillAppear-UIViewController对象的视图即将加入窗口时调用: viewDidApper-UIViewContro ...
- iOS9 集成指纹解锁
添加依赖库 LocalAuthentication.framework #import <LocalAuthentication/LocalAuthentication.h> // 头文件 ...