LeetCode Gas Station 两个特性,两种方法完美解答-更新证明方法

来源:互联网 发布:手机淘宝自定义链接 编辑:程序博客网 时间:2024/06/02 17:53

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.

这道题也挺麻烦的。乍看不难,用最简单的算法就是一个一个点地计算,计算到没油了,证明这点不能作为出发点。移动到下一个点作为出发点。这样的话思路还是挺简单的,不过这样写不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就是答案了。

程序二:跳跃式,跃过不能作为出发点的点,加速循环

不总结出这些特性是难做出来的。两种方法的运行时间都差不多。

程序一:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. class Solution {  
  2. public:  
  3.     int canCompleteCircuit(vector<int> &gas, vector<int> &cost)   
  4.     {  
  5.         int sum = 0;  
  6.         int total = 0;  
  7.         int j = -1;  
  8.         for(int i = 0; i < gas.size() ; ++i)  
  9.         {  
  10.             sum += gas[i]-cost[i];  
  11.             total += gas[i]-cost[i];  
  12.             if(sum < 0)  
  13.             {  
  14.                 j=i; sum = 0;   
  15.             }  
  16.         }  
  17.         if(total<0) return -1;  
  18.         else return j+1;  
  19.     }  
  20. };  

程序二:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. class Solution {  
  2. public:  
  3.     int canCompleteCircuit(vector<int> &gas, vector<int> &cost)   
  4.     {  
  5.         int n = gas.size();  
  6.         int j = 0;  
  7.         for(int i=0; i<n;)  
  8.         {  
  9.             j=i;  
  10.             if(startPoint(gas, cost, j))  
  11.                 return i;  
  12.             i += j;  
  13.         }  
  14.         return -1;  
  15.     }  
  16.    
  17.     bool startPoint(vector<int> &gas, vector<int> &cost, int& start)  
  18.     {  
  19.         int n = gas.size();  
  20.         int left = 0;  
  21.         int temp;  
  22.         for(int i=start; i<(n+start); i++)  
  23.         {  
  24.             temp = i%n;  
  25.             left += gas[temp]-cost[temp];  
  26.             if(left<0)   
  27.             {  
  28.                 start = i-start+1;  
  29.                 return false;  
  30.             }  
  31.         }  
  32.         return true;  
  33.     }  
  34. };  

第一种代码简单,却比较难想出来,第二种还比较好想出来吧。
我想想到底如何对付这些题目呢?尤其如果面试的时候,时间又受限,那更高难度了。

我想到的策略就是:先举些列子,观察他们的特性,然后总结出来,再设计算法吧。谁没做过,能一下子就看出其中的规律吗?

更新第二种思路的更加简洁点的代码:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. int canCompleteCircuit2(vector<int> &gas, vector<int> &cost)   
  2.     {  
  3.         for (int i = 0; i < cost.size(); )  
  4.         {  
  5.             int leftGas = 0;  
  6.             int j = 0;  
  7.             for (; j < cost.size(); j++)  
  8.             {  
  9.                 int k = (i+j)%cost.size();  
  10.                 leftGas += (gas[k] - cost[k]);  
  11.                 if (leftGas < 0) break;  
  12.             }  
  13.             if ( j == cost.size()) return i;  
  14.             i+=j+1;  
  15.         }  
  16.         return -1;  
  17.     }  

 证明的时候,使用反证法会简单点。

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //2014-2-18 update  
  2.     int canCompleteCircuit(vector<int> &gas, vector<int> &cost)   
  3.     {  
  4.         for (int i = 0; i < gas.size(); )  
  5.         {  
  6.             int left_gas = 0 , j = 0;  
  7.             for ( ; j < gas.size(); j++)  
  8.             {  
  9.                 int t = (i+j)%gas.size();  
  10.                 left_gas = left_gas + gas[t] - cost[t];  
  11.                 if (left_gas < 0) break;  
  12.             }  
  13.             if (j == gas.size()) return i;//错误:j=i?  
  14.             else i += j+1;//j+1,计算好下标  
  15.         }  
  16.         return -1;  
  17.     }  
0 0
原创粉丝点击