[Leetcode]Some thoughts on Q134

Definitely not the best solution... Just some thoughts help myself to understand.


Let's define** P(i,j) as the probability that the train can leave station i for station j before run out of gas. In this problem, we know P should either be 1 (can arrive) or 0 (cannot arrive). Now the probability that we start from station 0, through N stations finally arrive station N is presented by P(0,N).
Now let station k be the station between station 0 and station N (0<k<N), then it is easy to see P(0,N)=P(0,k)P(k,N)
,which means as long as one interval is unachievable, the whole journey from station 0 to station N is unachievable.Now with the same reason, we could say if P(0,N)=0 due to P(k,N)=0, is also unachievable.In the other word, if station 0 cannot be the start point to station N due to the problematic point station k, then the station m between
* station 0** and station k can also not be the start point. So the optimization way is we do not need to calculate the whole routine for each assumed start point, as long as we have a** start point, an end point and the problematic point k, we can directly reject all points from start point** to k point.
Now in this problem, assume there must be one achievable train routine, our choice is actually very limited. So by rejecting the enough choices, we could quickly find the answer.

Like the illustration above, we could simplify this problem as moving a rectangle of size N+1 with its starting point limited in area 0 to N. We calculate the sum from left to right, as long as the sum goes from positive to negative, for example, sum from 0 to z is positive, but from 0 to z+1 is negative, we could say P(0,z) suddenly goes to negative when becomes P(0,z+1). Since we have already proved that P(0,N)=P(0,k)P(k,N)*, so the only possibility causing the change from P(0,z)=1 to P(0,z+1)=0 is P(z,z+1)=0.So as we proved we could reject all the points from 0 to Z as starting points. Then we choose point z+1 as the new starting points and repeat the above process. Notice since the rectangle has size N+1, so we only need to check the sum of at most N+1 points.
Finally the first point we are unable to reject and is within first N+1 points is the answer. Otherwise, there is no such situation that train can travel all through. The complexity is O(n). ( n = number of the station).

class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int count=0;
        int sum=0;
        int start=0;
        int target_size=gas.size();
        for(int i=0;i<gas.size()&&start<target_size;i++){
            sum+=gas[i]-cost[i];
            gas.push_back(gas[i]);
            cost.push_back(cost[i]);
            count++;
            if(sum<0){
                if(count==1){
                    start=i+1;
                }
                else{
                    start=i;
                    i--;
                }
                count=0;
                sum=0;
            }
            else if(count==target_size){
                return start;
            }
            
        }
        return -1;  
        
    }
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 2017年6月2日 星期五 天气阴 今天晚上,我和我妈从广场回来,我有点冷就在那搓手,然后我又问我妈为什么石头从...
    王鑫隆阅读 1,108评论 0 0
  • 姚安的天气总是那么好,是吧。这种感觉很舒服。 世界上最大的激励莫过于你去努力之后发现原来看起来很难的事情你也可以做...
    稻城禾欢阅读 1,391评论 0 0

友情链接更多精彩内容