Can anybody explain me the sample input case? how we got 7?

acc to me , ans should be 5 , as only 1 litre petrol is needed to reach at next point …next point is end point … we reach there … so 5*1 =5 :confused:

@Muskan-Gupta-598128740703036,
It also has to cross (n-1)th checkpoint to reach the destination,
I know Also checkpoint N−1 will lead to his final destination., is ambiguous, but it means, after crossing (n-1)th checkpoint, you will reach destination.

whats wrong with the code?? giving wrong ans :frowning:

code is in java lang

@Muskan-Gupta-598128740703036,
https://ide.codingblocks.com/s/225769 corrected code.

This is a greedy problem.

We just need to buy petrol from the minimum possible rates checkpoints.

Lets say we are at checkpoint X. Now there are X+1 checkpoints up to this checkpoint.

We can easily see that for buying petrol for going to checkpoint X+1 , we just need to find the minimum possible C[i] where i can be from 1 to X+1 .

Thus, we can just compare the minimum petrol rate till now(lets say denote it by (temp) at each of the checkpoint with the checkpoint rate i.e. C[i] . Now two possible cases arise:

  • First Case: temp <= C[i], No need to update temp i.e. petrol for this checkpoint has to be bought with the rate .

  • Second Case: temp > C[i] , We need to update temp and petrol for this checkpoint has to be bought with the new rate i.e. C[i] .

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.