Advertisement
imashutosh51

Circular tour

Jul 31st, 2022 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. The structure of petrolPump is
  2. struct petrolPump
  3. {
  4.     int petrol;
  5.     int distance;
  6. };*/
  7. /*Method 1: T. C O(2N)
  8. 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
  9. 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
  10. 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,
  11. 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
  12. next pumps so when car starting from the middle pumps will come to current node,then again they will not reach the next pump
  13.  
  14. eg. (5,6) (6,7) (7,4) (8,10) (6,6)  -->first is distance and second is petrol
  15.    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
  16.    so if you start at index 1 as start then you will loose even on surplus given by 0th index,so you will left
  17.    will only 1 surplus with at i=2 so middle indexes between previous start to current failed index,can not help to cross current
  18.    petrol pump ie. i=2
  19.    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
  20.    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.
  21. */
  22. class Solution{
  23.   public:
  24.     int tour(petrolPump p[],int n){
  25.         int start=0,i=0,surplus=0;
  26.         for(int i=0;i<n;i++){
  27.             if(p[i].petrol+surplus>=p[i].distance){surplus=surplus+p[i].petrol-p[i].distance;}
  28.             else{
  29.                 start=i+1;
  30.                 surplus=0;
  31.             }
  32.         }
  33.         i=start,surplus=0;
  34.         do{
  35.           if(p[i].petrol+surplus>=p[i].distance){
  36.               surplus=surplus+p[i].petrol-p[i].distance;
  37.               i++; i%=n;
  38.           }
  39.           else return -1;
  40.         }while(i!=start);
  41.        
  42.         return start;
  43.     }
  44. };
  45.  
  46. /*Method 2: T.C(O(N))
  47. In this method we can avoid the last do while loop.
  48. 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
  49. 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,
  50. 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
  51. 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
  52. cirlcle else no starting point
  53. */
  54. class Solution{
  55.   public:
  56.     int tour(petrolPump p[],int n){
  57.         int start=0,i=0,surplus=0,sum=0;  //sum is the summation of required petrol units at deficit pumps
  58.         for(int i=0;i<n;i++){
  59.             if(p[i].petrol+surplus>=p[i].distance){surplus=surplus+p[i].petrol-p[i].distance;}
  60.             else{
  61.                 sum+=(p[i].distance-(p[i].petrol+surplus));
  62.                 start=i+1;
  63.                 surplus=0;
  64.             }
  65.         }
  66.         if(surplus>=sum) return start;
  67.         else return -1;
  68.     }
  69. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement