【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.
地址:
https://oj.leetcode.com/problems/gas-station/
思路:
O(n^2) 的不用闲扯,谁都懂。问题是O(n) 的算法应该如何设计。方法如下:
定义:
存油:卡车带进某个加油站的油量。依题意,初始存油为0.
0、设定初始起点和终点为0.
1、从起点开始,走到不能走为止,设此时不能走的结点为i。则0到i之间的所有结点都不可能为起点。因为从起点开始只有0升油,从起点经过的结点存油可能为正值但至少为0,所以如果以0存油开始,到i一样过不去。
2、起点倒退,追踪i点存油量,直到能过i点为止。
3、回到第一步
证明:
每个结点最多访问一次,O(n)
AC代码:
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int start = 0;
int fin = 0;
int oil = 0; // 带入的油
int tmpoil = 0;
int len = gas.size();
if (fin == lastPos(start,len))
return gas[0] >= cost[0] ? 0 : -1;
do
{
int tmpfin = fin; // 我在tmpfin能前进吗?
while (tmpfin != lastPos(start,len) && ((tmpoil = walk2next(oil,tmpfin,gas,cost))) >= 0)
{
tmpfin = nextPos(tmpfin,len);
oil = tmpoil;
}
if (tmpfin == lastPos(start,len))
return start;
fin = tmpfin;
do
{
start = lastPos(start,len);
if (start == fin)
return -1;
int tmp = gas[start] - cost[start]; // tmp指的是具体某点0油出发到下一点后的剩余油量
tmpoil += tmp;
} while (tmpoil < 0);
oil = tmpoil; // 补到能够进入下一点为止,此时oil为补够了的剩余量
fin = nextPos(fin,len); // 进入下一点
} while (fin != start); // 由于进入了下一点,所以为start的话,就成功。
return start;
}
int lastPos(int index,int len)
{
if (index == 0)
return len - 1;
else
return index - 1;
}
int nextPos(int index,int len)
{
return (index + 1) % len;
}
int walk2next(int oil,int index,vector<int> &gas,vector<int> &cost)
// 带有oil的油量进入index,要走的下个点缺/剩多少油
{
int total = oil + gas[index];
return total - cost[index];
}
};
后记:
当你用while很艰难时,请用do while
【leetCode百题成就】Gas Station解题报告的更多相关文章
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- LeetCode: Gas Station 解题报告
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】743. Network Delay Time 解题报告(Python)
[LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)
[LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...
- 【LeetCode】729. My Calendar I 解题报告
[LeetCode]729. My Calendar I 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar- ...
随机推荐
- Android获得Manifest在<meta-data>元件的值
前段时间攻略完成游戏开发项目.其中用于包装散装. 目前市场上的网络不提交.但是,通过设置Manifest中的Meta_data>去获得相关參数,游戏ID号改变.游戏ID改变,然后游戏内容就改变. ...
- 【C语言】写一个函数,实现字符串内单词逆序
//写一个函数,实现字符串内单词逆序 //比如student a am i.逆序后i am a student. #include <stdio.h> #include <strin ...
- sd nfrmtl
http://www.zhihu.com/collection/24337307 http://www.zhihu.com/collection/24337259 http://www.zhihu.c ...
- Android.mk编译.apk .so .jar .a第三方.apk .so .jar .a的方法
一.编译一个简单的APK LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # Build all java files in the java s ...
- 西南民大oj(递推)
我的数学不可能那么难推 时间限制(普通/Java) : 3000 MS/ 9000 MS 运行内存限制 : 65536 KByte总提交 : 49 测试通过 : ...
- SVN Error: “' 'x' isn't in the same repository as 'y' ” during merge (并不在同一个版本库中)
在使用svn merge命令报错 英文版本:SVN Error: “' 'x' isn't in the same repository as 'y' ” during merge 中文版本报错:并不 ...
- 怎样使用 iOS 7 的 AVSpeechSynthesizer 制作有声书(1)
原文: http://www.raywenderlich.com/64623/make-narrated-book-using-avspeechsynthesizer-ios-7 随着 PageVie ...
- JsonCpp Documentation
JsonCpp - JSON data format manipulation library JsonCpp Documentation 0.6.0-rc2 Introduction JSON (J ...
- 收藏的一段关于java大数运算的代码
收藏的一段关于java大数运算的代码: package study_02.number; import java.math.BigDecimal; import java.math.BigIntege ...
- 【Android UI】案例03滑动切换效果的实现(ViewPager)
本例使用ViewPager实现滑动切换的效果.本例涉及的ViewPager.为android.support.v4.view.ViewPager.所以须要在android项目中导入android-su ...