We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Thanks for this. This is an elegant idea. Because of this I can agree that this problem can be used to practice queue data structure. Following this idea I have implemented following,
publicclassTruckTour{publicQueue<int>PPQueue;publicvoidTakeInput(){PPQueue=newQueue<int>();intn=int.Parse(Console.ReadLine());while(n-->0){string[]tokens=Console.ReadLine().Split();PPQueue.Enqueue(int.Parse(tokens[0])-int.Parse(tokens[1]));}}// get 0 based index of starting Petrol PumppublicintGetStartingPumpIndex(){// start with an initial queue// start index is 0// keep popping items// whenever it fails forward it to the next tage..intpass_count=0;ints_fa=0;intpp_start_index=0;while(pass_count<PPQueue.Count){intr_fa=PPQueue.Dequeue();// residue of fuel amounts_fa+=r_fa;if(s_fa<0){s_fa=0;pp_start_index+=pass_count+1;pass_count=0;}elsepass_count++;PPQueue.Enqueue(r_fa);}returnpp_start_index;}}
Truck Tour
You are viewing a single comment's thread. Return to all comments →
Thanks for this. This is an elegant idea. Because of this I can agree that this problem can be used to practice queue data structure. Following this idea I have implemented following,
full source-code link
For better understanding it's good to look at 2 goals specified by the problem description,