leetcode — gas-station
/**
* Source : 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.
*/
public class GasStation {
/**
*
* 判断车能否从某一个station开始绕所有的station走一圈
*
* sum(gas[i]-cost[i]) < 0表示一定不能走完,否则一定可以
*
* 如果能绕一圈,那么怎么寻找起点
*
* 性质1:如果从i出发可以绕一圈,那么从i出发可以达到任意station,显而易见
* 性质2:如果这样的i是唯一的,那么不存在 j!= i,从j出发能达到i,证明:使用反证法:假设这样的j存在,那么j能到达i,
* 由性质1得i可以到达任意station,那么i也可以到达j,那么就可以从j出发到达j,也就是说同时存在i、j可以绕一圈,与唯一解矛盾,所以不存在
* 性质3:假如i是唯一的解,那么从0至i-1出发无法到达i,从i出发可以到达i+1至n-1
*
* 结合以上三条性质,解法如下:
* 0:如果sum(gas[i]-cost[i]) < 0表示无解,否则有解,进入1
* 1:从0开始计算sum(gas[i]-cost[i]),如果sum < 0,则由0作为起点不能到达i,根据性质1,0不能作为起点,因为从0出发可以到达i,
* 由性质2,1至i-1不能作为起点,那么i+1作为新的候选起始点
* 3:以此类推,直到遇到k,从k出发能到达n-1,那么k就能绕一圈,因为这个时候k能到达n-1,加上sum(gas[i]-cost[i]) > 0一定有解的话,k就是起点
*
* @param gas
* @param cost
* @return
*/
public int canCOmpleteCircuit (int[] gas, int[] cost) {
int start = 0;
int sum = 0; // 从0开始的所有gas[i] - cost[i]和
int sumK = 0; // 从k开始的所有gas[i] - cost[i]和
for (int i = 0; i < gas.length; i++) {
sum += gas[i] - cost[i];
sumK += gas[i] - cost[i];
if (sumK < 0) {
start = i+1;
sumK = 0;
}
}
if (sum < 0) {
return -1;
}
return start;
}
}
leetcode — gas-station的更多相关文章
- [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 @ Python
原题地址:https://oj.leetcode.com/problems/gas-station/ 题意: There are N gas stations along a circular rou ...
- 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 ...
随机推荐
- 如何给小学生讲清楚ECC椭圆曲线加密
对于RSA这套公私钥加密的思路,我以为我挺明白的,运用的娴熟自如. 当然现在RSA用的不多,而是基于ECC曲线来做签名验签,最大名鼎鼎的莫过于比特币. 可是前两天和别人讲代码,被问了ECC为什么可以用 ...
- HTTP协议头部与Keep-Alive模式详解(转)
转自:http://a280606790.iteye.com/blog/1095085 http1.1 中怎么打开持久连接,怎么关闭,怎么传输数据(确定本次数据是否传输完毕) 1.什么是Keep-Al ...
- ios 上下滑动粘滞问题
ios 移动端,当你触及到可以左右滑动部分,进行上下滑动操作时,会导致上下滑动粘滞卡顿的问题 mdn:https://developer.mozilla.org/zh-CN/docs/Web/CSS/ ...
- Python数据科学手册
Python数据科学手册(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1KurSdjNWiwMac3o3iLrzBg 提取码:qogy 复制这段内容后打开百度网盘手 ...
- 《SpringMVC从入门到放肆》十三、SpringMVC数据校验
上一章,我们学习了SpringMVC的自定义类型转换器,但是如果转换后的数据传递到Controller的方法中,忽然发现有某些属性为Null了,这怎么办?我们需要一种有效的数据校验机制,来对数据进行有 ...
- WebService常用接口链接(很全面,值得一看)
天气预报Web服务,数据来源于中国气象局Endpoint :http://www.webxml.com.cn/WebServices/WeatherWebService.asmxDisco ...
- 神经网络_线性神经网络 1 (Nerual Network_Linear Nerual Network 1)
2019-04-08 16:59:23 1 学习规则(Learning Rule) 1.1 赫布学习规则(Hebb Learning Rule) 1949年,Hebb提出了关于神经网络学习机理的“突触 ...
- getMemory的经典例子
//NO.1:程序首先申请一个char类型的指针str,并把str指向NULL(即str里存的是NULL的地址,*str为NULL中的值为0),调用函数的过程中做了如下动作:1申请一个char类型的指 ...
- 微信小程序写tab切换
微信小程序之tab切换效果,如图: 最近在学习微信小程序并把之前的公司app搬到小程序上,挑一些实现效果记录一下(主要是官方文档里没说的,毕竟官方文档只是介绍功能) .wxml代码: <view ...
- SDL 开发实战(四): SDL 事件处理
在前面学习SDL的例子运行时,我们发现我们的窗口只停留了几秒,但是如果设置更长时间显然也有其他的弊端. 那么有没有一种好的办法可以解决这个问题呢?例如:能不能让窗口一直显示,直到检测到用户用鼠标点击关 ...