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.
- Prepare
- Data Structures
- Queues
- Truck Tour
- Discussions
Truck Tour
Truck Tour
Sort by
recency
|
366 Discussions
|
Please Login in order to post a comment
Save the previous circle you already calculated, in a way you can speed it up via memoization. After each pump you can add that pump's total cost to the cost of the pumps before it. Using this strategy you will never have to recalclate the beginning of the loop.
There's probably other ways to optimize it, but I got lazy after memoizing the beginning part of the loop. This way you never have to worry about trying to loop back your array around.
Im getting an error with this:
int fuel = 0; int miles = 0; int index = 0; for (int i = 0; i < petrolpumps.size(); i++) { fuel += petrolpumps.get(i).get(0); miles += petrolpumps.get(i).get(1); if (fuel < miles) { fuel = 0; miles = 0; index = i + 1; } } return index;
However if I change the inequality to this "if (fuel - miles < 0)" ,it is passing all the test cases. They are both supposed to be the same according to linear inequalities. This is math defying. Please help.The problem is misleading, as it does not crearly states that there will always be a solution. You are led to believe to check if you go the full circle. You usually are taught to think of all scenarios, including no solution. So this was a very vague problem description. Finally, I was able to do it by using a prefix list in O(n) time, by using tabulation to optimize checking all the posible circles.