LeetCode Gas Station 两个特性,两种方法完美解答-更新证明方法
2013-11-03 17:07
447 查看
Gas Station
There are N gas stations along a circular route, where the amount of gas at station i isgas[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.
这道题也挺麻烦的。乍看不难,用最简单的算法就是一个一个点地计算,计算到没油了,证明这点不能作为出发点。移动到下一个点作为出发点。这样的话思路还是挺简单的,不过这样写不accepted的,因为编译超时。
我觉得做这道题的关键是要可以总结出来这道题目的属性,注意Note这个地方,其属性主要有两个:
1 如果总的gas - cost小于零的话,那么没有解返回-1
2 如果前面所有的gas - cost加起来小于零,那么前面所有的点都不能作为出发点。
2013-12-1 update:
原创: 靖心http://write.blog.csdn.net/postedit/14106137
第一个属性的正确性很好理解。那么为什么第二个属性成立呢?
首先我们是从i =0个gas station计算起的,设开始剩油量为left=0,如果这个station的油是可以到达下一个station的,那么left=gas-cost为正数,
到了下一个station就有两种情况:
1 如果i=1个station的gas-cost也为正,那么前面的left加上当前station的剩油量也为正。
2 如果i=1个station的gas-cost为负,那么前面的left加上当前的station的剩油量也有两种情况:
一) left为正,那么可以继续到下一个station,重复前面计算i=2,i=3...,直到发生第二)种情况
二)如果left为负,那么就不能到下一个station了,这个时候如果i=k(i<k<n),这个时候是否需要从第i=1个station开始重新计算呢?不需要,因为第k个station之前的所有left都为正的,到了第k个station才变成负。
证明:
left(i)>0, 如果left(i+1)<0,从i+1个station重新开始测试是没有必要的;如果left(i+2) > 0呢? 那么left(i) + left(i+1)>0; left(i) + left(i+1) +left(i+2) > left(i+2)那么从i+2个station开始也是没有必要的,以此类推……left(i) + ...+ left(k-2)>0, 那么left(i)+...+left(k-2) > left(k-1), 那么就是没有必要从第k-1个节点重新开始计算了,现在到了第k个station的剩油量left变为负,也不能作为出发点,那么直接到k+1个计算就可以了。这就可以得出属性2了。
以前没重视数学的证明定理的方法,要去证明一个定理是很困难的。但是原来证明的方法主要不是用来证明定理的,而是用来发现规则和特征的。
主要利用属性2可以写两个程序:
程序一:记录最后一个加起来小于零的索引,然后返回这个索引+1就是答案了。
程序二:跳跃式,跃过不能作为出发点的点,加速循环
不总结出这些特性是难做出来的。两种方法的运行时间都差不多。
程序一:
class Solution { public: int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int sum = 0; int total = 0; int j = -1; for(int i = 0; i < gas.size() ; ++i) { sum += gas[i]-cost[i]; total += gas[i]-cost[i]; if(sum < 0) { j=i; sum = 0; } } if(total<0) return -1; else return j+1; } };
程序二:
class Solution { public: int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int n = gas.size(); int j = 0; for(int i=0; i<n;) { j=i; if(startPoint(gas, cost, j)) return i; i += j; } return -1; } bool startPoint(vector<int> &gas, vector<int> &cost, int& start) { int n = gas.size(); int left = 0; int temp; for(int i=start; i<(n+start); i++) { temp = i%n; left += gas[temp]-cost[temp]; if(left<0) { start = i-start+1; return false; } } return true; } };
第一种代码简单,却比较难想出来,第二种还比较好想出来吧。
我想想到底如何对付这些题目呢?尤其如果面试的时候,时间又受限,那更高难度了。
我想到的策略就是:先举些列子,观察他们的特性,然后总结出来,再设计算法吧。谁没做过,能一下子就看出其中的规律吗?
更新第二种思路的更加简洁点的代码:
int canCompleteCircuit2(vector<int> &gas, vector<int> &cost) { for (int i = 0; i < cost.size(); ) { int leftGas = 0; int j = 0; for (; j < cost.size(); j++) { int k = (i+j)%cost.size(); leftGas += (gas[k] - cost[k]); if (leftGas < 0) break; } if ( j == cost.size()) return i; i+=j+1; } return -1; }
证明的时候,使用反证法会简单点。
//2014-2-18 update int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { for (int i = 0; i < gas.size(); ) { int left_gas = 0 , j = 0; for ( ; j < gas.size(); j++) { int t = (i+j)%gas.size(); left_gas = left_gas + gas[t] - cost[t]; if (left_gas < 0) break; } if (j == gas.size()) return i;//错误:j=i? else i += j+1;//j+1,计算好下标 } return -1; }
相关文章推荐
- 章节1:SQL语言简易入门
- 章节0:MySQl学前知识储备
- hibernate 关联关系映射
- 四。向内核添加内核模块
- Servlet基础(三) Servlet的多线程同步问题
- Servlet基础(二) Servlet的生命周期
- 利用cookie 记住背景颜色 (利用Jquery对Cookie进行设值与取值)
- 机器学习中的超平面wx+b=0?
- C# ZIP文件的压缩和解压缩(SharpZipLib.dll)
- yii cgridview 如何显示图片
- 几何对象和空间参考
- 向亲人、故乡和“本民族”致敬的写作----查舜长篇小说《月亮是夜晚的一点明白》读后
- 遇到的比较有意思的问题(1)去掉重复数字(2)提取数组的子数组,求和最大的子数组、最大的子数组和
- Servlet基础(一) Servlet基础和关键的API介绍
- [原]考试系统——C#反射机制+工厂模式+缓存机制遇到问题
- [原]考试系统——C#反射机制+工厂模式+缓存机制遇到问题
- hibernate 配置搭建
- 插入排序
- 【Java TCP/IP Socket】TCP Socket(含代码)
- Jquery 秀、无极限------正则表达式、Html、CSS、JavaScript基础回顾