Gas Station
2015-10-11 13:12
183 查看
题目: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.思路:
这道题首先是用的贪心算法,有几点需要注意:优先选择所有加起来,sum>0的。如果能够保证从某一点到最后,sum一直大于0,说明从这个点到最后一个点没问题,
但这个时候,需要注意的是未必从前面能够走到index这个点,所以返回头继续判断,但升级版的程序就很简短,total一直做的工作就是累加
不过需要证明一圈下来sum>0,一定存在某一个解。
第一种:
假设从0到n-1,从某点a,b,c,....,a,a不能反悔,恰好停留在b,然后b不能到c,一切很巧,
sum[gas[a...b]]<sum[cost[a...b],sum[gas[b...c]]<sum[cost[b...c].....sum[gas[p...a]]<sum[cost[p...a]
sum[gas[a...b]]+sum[gas[b...c]]+sum[gas[p...a]]<sum[cost[a...b]+sum[cost[b...c]+sum[cost[p...a]
即 c*sum[gas]<c*sum[cost],与原文矛盾
第二种
s1,s2,....sn-1能构成,这里sj代表gai[i]-cost[i]
s1+s2+....+sn-1>0,那么存在某个s,使得s> -1(s1+s2+....+sj-1)+ (-1)(sj+1+....+sn-1),也可以是连续的两项,都可以,即只是存在某一个解
另外,某些所谓的动态规划其实和这种思路差不多。还需要证明的就是,从a点恰好到b点,但不能到达下一个点,那么a到b任何一个点都不能到达b的下一个点
证明:从a到a与b之间任何一点 gas[i]-cost[i]都是大于0的,不然不能到达,那么从这一点到b下一点一定是负,不然加起来就是正,前面已经是正数,加起来是负数,那么后面一定是负数。就是这么证明。
代码:
//本题的集中解法本质上都一样。//https://leetcode.com/problems/gas-station/
class Solution1 {
public:
//http://m4tiku.duapp.com/report?pid=18
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int startIndex=0;
int sum=0;
for(int i=0;i<=gas.size()-1;i++){
sum=sum+(gas[i]-cost[i]);
if(sum<0){
startIndex=i+1;
sum=0;
}
}
//判断是不是到了末尾
if(startIndex==gas.size()){
return -1;
}
//从startIndex之后都没有问题,但是不知道,从结尾到startIndex是不是有问题。
for(int i=0;i<startIndex;i++){
//就相当于确定了后面,继续确定前面
sum=sum+(gas[i]-cost[i]);
if(sum<0){
return -1;
}
}
return startIndex;
}
};
class Solution2 {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int sum=0,start,total=0;
for(int i=0;i<gas.size();i++){
sum=sum+gas[i]-cost[i];
total=total+gas[i]-cost[i];
if(sum<0){
sum=0;
start=i+1;//找到开始节点
}
}
return total>=0?start:-1;
}
};
相关文章推荐
- BestCoder Round 59 (HDOJ 5500) Reorder the Books
- hdu4770 暴力状压 Lights Against Dudely (2013 Asia Hangzhou Regional Contest problem A)
- 2台主机极致实现双主复制架构及MMM
- jQuery源码测试笔记之domManip函数
- Android开机启动程序
- 提供应用10倍性能的10个手段
- Android IOS WebRTC 音视频开发总结(四五)-- ORTC背后的真相
- 欢迎使用CSDN-markdown编辑器
- 第二阶段
- Java基础知识强化之IO流笔记45:IO流练习之 把集合中的数据存储到文本文件案例
- 第六周项目四数据结构实践——数制转换(栈)
- 全透明Activity
- 在Unity中添加Googole广告
- 呵呵,双城记
- You must restart adb and Eclipse的问题解决
- 第二章 Winsock编程接口(1)
- 黑马程序员———java 集合框架
- 91-100例
- MyBatis学习笔记-第一天
- Java加密解密技术系列之DES