[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 ...
随机推荐
- 两次fopen不同的文件返回相同的FILE* 地址
最近接触一个垃圾程序,出现一个奇怪的bug,现象是两次fopen不同的文件返回相同的FILE*地址,第二次返回的FILE*有时候无端端的就被关闭了.以下代码是对这个bug的概括: auto fp1 = ...
- 大型系统OA--需求
1.面向普通用户 普通用户一般都是公司的小职员,上OA都干什么呢?当然是获取信息咯.还有最好很多事儿,不需要跟公司的其他部门的人儿,打交道,直接在网上解决就好了. 收发邮件: 查看自己发动的流程,也就 ...
- 分布式文件系统FastDFS安装与配置(单机)
安装包如下:fastdfs-nginx-module_v1.16.tar.gzFastDFS_v5.05.tar.gzlibfastcommon-master.zipnginx-1.8.0.tar.g ...
- 踩过的坑之-----selector
打算踏踏实实的做技术了,以前总是毛毛躁躁的将代码粘贴复制完事能跑起来就行.最近慢慢感觉这样真的对自己的时间和经历是一种浪费. 就从最基本的做起吧,今天做了一个selector,在按钮上面添加效果, & ...
- HTML Meta, http-equiv, Refresh
原文: http://www.lifelaf.com/blog/?p=481 在HTML页面中,如果想实现定时刷新或重定向,我们可以使用meta标签的refresh功能: <!-- 5秒后刷新页 ...
- AC自动机——多模式串匹配的算法思想
标准KMP算法用于单一模式串的匹配,即在母串中寻求一个模式串的匹配,但是现在又存在这样的一个问题,如果同时给出多个模式串,要求找到这一系列模式串在母串存在的匹配个数,我们应该如何处理呢? 基于KMP算 ...
- 登陆用户怎样获取验证码和保存用户到cookie中
User表: User.java package user.domain; import java.io.Serializable; import java.util.Date; public cla ...
- BufferedInputStream,FileInputStream,FileChannel实现文件拷贝
从上篇文章中知道BufferedInputStream是自带缓冲区的输入流,可以大大减少IO次数,提供效率.下面的例子中实现了用BufferedInputStream与FileInputStream实 ...
- volley使用与解析(一)
1.什么是volley Volley是google发布的基于Android平台上的网络通信库,能使网络通信更快,更简单,更健壮.获取地址:git clone https://android.googl ...
- GWT(Google Web Tookit) Eclipse Plugin的zip下载地址(同时提供GWT Designer下载地址)
按照Eclipse Help->Install new software->....(这里是官方安装文档:http://code.google.com/intl/zh-CN/eclipse ...