LeetCode——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.
贪心or动态规划?
从gas[0]开始走,遇到油不够的情况,开始station就后退一步,利用以前算的结果可以只算出第一步的油量。直到最后,算出circle的连接点是否满足。
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int n = gas.length;
int start = 0, end = 0, have = 0, cur = 0;
for(int i=0; i<n-1; i++) {
have += gas[cur] - cost[cur];
if(have >= 0) {
end ++;
cur = end;
}
else {
start --;
if(start < 0) {
start = n - 1;
}
cur = start;
}
}
have += gas[cur] - cost[cur];
return have>=0?start:-1;
}
}
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 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 ...
随机推荐
- 转:主流数据恢复软件——EasyRecovery/Ashampoo Undeleter/Wise Data Recovery/Recuva/Undelete 360
转自:Baidu 空间 2012-10-05 13:57 主流数据恢复软件——EasyRecovery/Ashampoo Undeleter/Wise Data Recovery/Recuva/Und ...
- mysql导入数据出错
今天准备移动网站到另外一个服务器,用的是mysql数据库,导出的时候正常,导入的时候出现了一个错误,纠结了半天 最后打开sql文件发现导出的sql确实有问题 具体什么原因不清楚,只好把以前备份的sql ...
- gridlaylout 简单布局
package com.example.gridlayout; import android.app.Activity; import android.os.Bundle; import androi ...
- YAFFS2文件系统分析(转)
http://blog.chinaunix.net/uid-25314474-id-343665.html 1.前言略. 2.yaffs 文件系统简介按理说这里应该出现一些诸如“yaffs 是一种适合 ...
- clearfix清除浮动进化史
我想大家在写CSS的时候应该都对清除浮动的用法深有体会,今天我们就还讨论下clearfix的进化史吧. clearfix清除浮动 首先在很多很多年以前我们常用的清除浮动是这样的. .clear{cle ...
- Windows调试学习笔记:(一)WinDBG中加载SOS和CLR
最近产品环境出现了部分服务器当机情况,虽然重启之后问题就不出现了.但本着彻底解决问题的态度,想要找到root cause.多次尝试Visual Studio失败(可能是代码惊醒了优化和签名)之后,决定 ...
- SQL SERVER 安全性体系
主体和安全实体 在 SQL Server 2008中,“主体”就是可以访问受保护资源且能获得访问资源所需权限的任何个人.组或流程.与旧版 SQL Server 一样,可以在 Windows 中定义主体 ...
- EZGUI下的动态图片的处理
EZGUI的使用过程中,有时需要使用动态的图片,比如商店里面商品的ICON,好友的头像等,通过使用SimpleSprite可以实现这个功能. 比如一个通过网络显示好友头像: WWW www = n ...
- Android实现自适应正方形GridView(陌陌引导页面效果)
1.http://blog.chengyunfeng.com/?p=465 2.备注,慢慢研究
- 使用hexo在github上写blog
使用hexo在github上写blog 安装nodejs http://nodejs.org/ 安装hexo npm install -g hexo 创建bolg文件夹 安装完成后在自己的工作目录创建 ...