[Leetcode] maximum sub-sequence of gas station and greedy

Source: Internet
Author: User

There are N gas stations distributed in a circular path, which consume oil from a gas station to the next gas station. To find a starting point, from this starting point, the storage volume> = fuel consumption. The solution must be unique.

This requires that the path should not be non-negative.

If you find the sub-sequence with the largest oil storage, you can make sure that the whole process is completed as much as possible (Greedy thinking ).

Therefore, you need to find the largest subsequence and in the ring array. The maximum subsequence and the maximum subsequence are classic DP problems. For the ring constraint, we use copying an array and placing it at the end of the original array, and then solving it using the normal maximum subsequence. In the meantime, you need to record the starting position (this is the final requirement), the number of sites that walk through (the number of sites cannot exceed N, which is a ring array with a length of 2n), the largest subsequence and. When the length is greater than N, you need to adjust the start position backward to meet the requirement that the start position is not a negative number.


Complexity O (2n) => O (N)


class Solution {public:    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {        vector<int>vt(gas.size()*2);        int i,mx=-1,sum=0,n=gas.size();        for(i=0;i<n;++i){            vt[i]=vt[i+n]=gas[i]-cost[i];            sum+=vt[i];        }        if(sum<0)return -1;        int m=n*2,j=0,st=0,mst=-1,step=0;        sum=0;        for(i=0;i<m&&st<n;++i){            sum+=vt[i];            ++step;            if(sum<0){                step=0;sum=0;st=i+1;                continue;            }            if(step>n){                j=st;sum-=vt[j++],step--;                while(j<i&&vt[j]<0)sum-=vt[j++],step--;                st=j;            }            if(sum>mx){                mx=sum,mst=st;            }        }        return mst;    }};

The maximum subsequences and problems of the ring array are described here. The maximum subsequences maxv and the minimum subsequences and MINV of the non-ring array are obtained, then the maximum subsequence and answer of the ring array are Max (maxv, total-MINV). If the total-MINV value is greater, in fact, the location is the next location of the minimum subsequence and the termination location.

int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {        int i,mx=0,sum=0,n=gas.size(),d,sum2=0,mn=0,end=0,tot=0,start=0,mst=0;        for(i=0;i<n;++i){            d=gas[i]-cost[i];            sum+=d;            tot+=d;            if(sum<0){                sum=0;start=i+1;            }            if(sum>mx){                mx=sum;mst=start;            }            sum2+=d;            if(sum2>0){                sum2=0;            }            if(sum2<mn){                mn=sum2;                end=i;            }        }        return tot<0?-1:(tot-mn>mx?(end+1)%n:mst);    }


[Leetcode] maximum sub-sequence of gas station and greedy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.