Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The structure of petrolPump is
- struct petrolPump
- {
- int petrol;
- int distance;
- };*/
- /*Method 1: T. C O(2N)
- Assume that first index is the index which will complete the circle so keep a pointer ie. start at it,and start traversing and if any point
- have petrol including surplus is less than the distance of next petrol pump,means that start can't be the starting point so one important
- observation if car can't go currnet pump to next petrol than any node in middle to start to current(inclusive) can't be the starting point,
- because the the pumps in the middle can provide surplus petrol to next pump or no surplus petrol to next pumps,but can't borrow petrol from
- next pumps so when car starting from the middle pumps will come to current node,then again they will not reach the next pump
- eg. (5,6) (6,7) (7,4) (8,10) (6,6) -->first is distance and second is petrol
- if start ==0,then at i=2 ie(7,4) here surplus is equal to 2 so 4+2<7 at i=3 ie. shortage of 1 unit petrol
- so if you start at index 1 as start then you will loose even on surplus given by 0th index,so you will left
- will only 1 surplus with at i=2 so middle indexes between previous start to current failed index,can not help to cross current
- petrol pump ie. i=2
- so make new start at i+1 and surplus as 0,and follow same steps if you crosses end of array,then there is possibilty that your choosen
- start is good because the car starting from choosen start can fail to o the indexes before it,so we will check for that start and go through wholse circle.
- */
- class Solution{
- public:
- int tour(petrolPump p[],int n){
- int start=0,i=0,surplus=0;
- for(int i=0;i<n;i++){
- if(p[i].petrol+surplus>=p[i].distance){surplus=surplus+p[i].petrol-p[i].distance;}
- else{
- start=i+1;
- surplus=0;
- }
- }
- i=start,surplus=0;
- do{
- if(p[i].petrol+surplus>=p[i].distance){
- surplus=surplus+p[i].petrol-p[i].distance;
- i++; i%=n;
- }
- else return -1;
- }while(i!=start);
- return start;
- }
- };
- /*Method 2: T.C(O(N))
- In this method we can avoid the last do while loop.
- As we know in previous example at i=2,car starting from i=0,fail so to cross the i=3,we need one unit of petrol so ofcourse we will get
- those petrol as surplus from some pump before index 0,if we sum all the surplus needed to cross those pumps like i=2 ans store in sum variable,
- these surplus petrol requires to cross the i=0 till before actual start.Now the we get the surplus getting after crossing the last index of array
- so if that surplus is greater than total surplus at deficit pumps then our start crossed the last petrol pump can be the starting point to complete
- cirlcle else no starting point
- */
- class Solution{
- public:
- int tour(petrolPump p[],int n){
- int start=0,i=0,surplus=0,sum=0; //sum is the summation of required petrol units at deficit pumps
- for(int i=0;i<n;i++){
- if(p[i].petrol+surplus>=p[i].distance){surplus=surplus+p[i].petrol-p[i].distance;}
- else{
- sum+=(p[i].distance-(p[i].petrol+surplus));
- start=i+1;
- surplus=0;
- }
- }
- if(surplus>=sum) return start;
- else return -1;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement