[LeetCode OJ] 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.
代码一:
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { //时间复杂度为O(n)
int total=;
unsigned i,j,start;
for(i=; i<gas.size(); )
{
start = i;
int residual = gas[i]-cost[i];
total += gas[i]-cost[i];
for(j =i+; j<gas.size(); j++)
{
if(residual<)
{
i=j;
break;
}
total += gas[j]-cost[j];
residual += gas[j]-cost[j];
}
i=j;
}
return total>=? start : -;
}
};
代码二:
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
unsigned start = ;
int current_gas = ;
int total_gas = ;
for(unsigned i=; i<gas.size(); i++)
{
current_gas += gas[i]-cost[i];
total_gas += gas[i]-cost[i];
if(current_gas<) //从第i站出发到第i+1站很耗油
{
start = i+;
current_gas = ;
}
}
return total_gas>= ? start : -;
}
};
代码一和代码二的思想是一样的,只是形式不太一样,相比较而言,代码二可读性更好。
问题分析:
如果sum(gas)>=sum(cost),则一定存在一个合适的站点,使得从该站点出发汽车可以转一圈再返回到起始点,但是起始点的唯一性并不能保障。
比如gas=[4,5,6,7],cost=[1,2,3,4],则任一站点都可以作为起始点。
所以本题中给出Note:
The solution is guaranteed to be unique.
如果sum(gas)<sum(cost),则不存在这样的起始点,这一点很容易想到。
代码思想:
假设有n个站点:S1,S2,S3,...,Sn,当前油箱内油量为0,从S1开始,判断从S1站点能否开到S2站点,如果可以的话说明达到S2站点时汽车内油量>=0,
我们标记S1>0,表示从S1可以到达S2;否则,标记S1<0。 当Si>0时,继续判断Si+1是否大于0,当Si<0时,说明当前设置的起始点不成功,将新的起始点
设为Si+1,判断从Si+1->Si+2->...->Sn是否成功。 如果从Si+1能否到达Sn,并且sum(gas)>=sum(cost),那么Si+1就可以作为起始点。
举个例子: S1, S2, S3, S, S5, S6, S7,..., Si, Si+1, Si+2,..., Sn 标记为绿色的表示在遍历过程中被设为起始点的站点
current_gas |>0 >0 <0 | >0 >0 >0 >0,...,<0 | >0 >0 ,..., >0|
| <0 | <0 | >0 |
sum(gas13)-sum(cost13)<0
[LeetCode OJ] Gas Station的更多相关文章
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 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
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- 【leetcode】Gas Station
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
- 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 _ 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]. 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 ...
随机推荐
- 【转】patch命令
原文网址:http://bbs.chinaunix.net/thread-1945698-1-1.html patch给文件1应用补丁文件变成另外一个文件2(需要先用"diff 文件1 文件 ...
- 【转】linux 中dd命令使用详解
原文网址:http://xiaozhuang.blog.51cto.com/4396589/850657 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究 ...
- 在C#中使用WIA获取扫描仪数据(利用Filter处理图片)
WIA Automation Layer不仅能从设备中捕获照片,还能进行简单的处理.当WIA Automation Layer从设备中捕获照片,保存为一个ImageFile对象,我们可以通过访问该Im ...
- Sicily1020-大数求余算法及优化
Github最终优化代码: https://github.com/laiy/Datastructure-Algorithm/blob/master/sicily/1020.c 题目如下: 1020. ...
- DB2 insert into 三种写法
db2的insert into 支持三种格式,即:一次插入一行,一次插入多行和从SELECT语句中插入. 以表为例: create table “user" ( "name&quo ...
- K倍动态减法游戏
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2580 #include <iostream> #include <string.h> ...
- Winform Windows Media Player 简易播放器 分类: WinForm 2014-07-31 20:12 589人阅读 评论(0) 收藏
新手上路,高手勿进! 窗体设计: 实现效果: 实现代码: using System; using System.Collections.Generic; using System.ComponentM ...
- C++程序在debug模式下遇到Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call问题。
今天遇到一个Access Violation的crash,只看crash call stack没有找到更多的线索,于是在debug模式下又跑了一遍,遇到了如下的一个debug的错误提示框: 这个是什么 ...
- Ubuntu开机出现:Fontconfig warning:"/etc/fonts/conf.d/65-droid-sans-fonts.conf"的解决办法
Ubuntu升级后可能会出现以下问题: Fontconfig warning: "/etc/fonts/conf.d/65-droid-sans-fonts.conf", line ...
- GDB调试总结__1
该博客旨在分享IT技术心得和实际工作中遇到问题的解决方法,下面是新浪博客地址http://blog.sina.com.cn/qianyumolu,则为分享经济.行业趋势.心灵文章等,有兴趣的朋友能够踩 ...