[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 ...
随机推荐
- 【转】单独编译android framework模块出现的问题
原文网址:http://blog.csdn.net/leonan/article/details/8629561 全编andorid后,单独修改编译一个framwork模块,make snod会有如下 ...
- 数据结构学习笔记——stack实现(数组篇)
一 栈:是一种表,限制插入和删除只能在一个位置,也即是表的末端(也是栈的顶)进行. 基本操作:push 和 pop. 二 栈的数组实现: 运用数组来存储元素,和栈操作先关的是theArray(一个数组 ...
- 通过例子学python(1)
第一章 基础知识 1.4 数字和表达式 # -*- coding: cp936 -*- # 1.4 数字和表达式 # ** 表示幂(乘方)运算 #1.4.1 长整型数 print(9999999999 ...
- November 4th Week 45th Friday 2016
Problems are not stop signs, they are guidelines. 问题不是休止符,而是指向标. Most of the problems can be overcom ...
- 设置U盘为第一启动顺序
本文转载:http://u.diannaodian.com/Article/1004.html 盘安装系统的首要步骤就是设置U盘为第一启动顺序.下面电脑店官网就来说说到底如何来设置U盘启动顺序吧. ...
- enum类型被intent所携带时需要注意的地方
一般我们在Activity之间传递对象时多用Parcelable.比如写一个class,在这个class上标明implements Parcelable并实现接口就可以用Intent.putExtra ...
- hdu 4911 Inversion(归并排序求逆序对数)2014多校训练第5场
Inversion Time Limit: 20 ...
- 实战DeviceIoControl 之中的一个:通过API訪问设备驱动程序
P.bhw98 { PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 9pt; PADDING-BOTTOM: 0px; MARGIN: 10px 0 ...
- android 47 service绑定
如果一个service已经启动了,activity和service绑定了在解除邦定,则这个service不会销毁,因为这个service不是这个Activity创建的. service生命周期: Ac ...
- COMPACT 行记录格式
CREATE TABLE `mytest` ( `t1` varchar() DEFAULT NULL, `t2` varchar() DEFAULT NULL, `t3` ) DEFAULT NUL ...