【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- ...
随机推荐
- CSDN编程挑战——《高斯公式》
高斯公式 题目详情: 高斯在上小学时发明了等差数列求和公式:1+2+..+100=5050.如今问题在于给你一个正整数n,问你他能够表示为多少种连续正整数之和?(自身也算). 输入格式: 多组数据,每 ...
- [置顶] CentOS release 5.4 (Final)重置root密码(图文)
- fopen()惹的祸
读一个文件,刚开始只读“r” 打开,读数据,刚开始的一段数据还好,但只读了一小部分就读不到正确的数据了,后来反复的看自己的代码,比对文件的内容,纠结了一天了都,感觉什么都没写错啊.心里总认为是这个文 ...
- 从零开始,创建GitHub团队开发环境
从零开始,创建GitHub团队开发环境 GitHub提供免费的团队环境,不过免费仓库容量是300MB,请大家注意. 申请GitHub个人账号 1. 使用浏览器访问GitHub主页.如果使用IE,尽量不 ...
- hdu3062(two-sat)
传送门:Party 题意:有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席.在2n 个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同时出现在 ...
- Amazon.com : The Odyssey of the Manual Toothbrusher
Amazon.com : The Odyssey of the Manual Toothbrusher The Odyssey of the Manual Toothbrusher
- ZOJ 1610 间隔染色段树
要长8000仪表板.间染色的范围,问:最后,能看到的颜色,而且颜色一共有段出现 覆盖段 数据对比水 水可太暴力 段树: #include "stdio.h" #include ...
- hdu4341(分组背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4341 题意:一个人在原点(0,0)抓金子,每块金子有一个获得需要的时间t和价值v.而且有的金子可能在一 ...
- (step6.1.4)hdu 1102(Constructing Roads——最小生成树)
题目大意:输入一个整数n,表示村庄的数目.在接下来的n行中,每行有n列,表示村庄i到村庄 j 的距离.(下面会结合样例说明).接着,输入一个整数q,表示已经有q条路修好. 在接下来的q行中,会给出修好 ...
- 通过 Jersey Http请求头,Http响应头,客户端 API 调用 REST 风格的 Web 服务
原地址:http://blog.csdn.net/li575098618/article/details/47853263 Jersey 1.0 是一个开源的.可以用于生产环境的 JAX-RS(RES ...