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.
I used a <vector> rather than a <queue>, and then just wrapped the index around to make it a circular queue.
Still a "queue-based solution" but a bit more efficient than repeatedly pushing and popping elements.
structPump{intfuel;intdistanceToNext;Pump(intf,intd):fuel(f),distanceToNext(d){}};boolgetTruckin(vector<Pump>&pumps,intstartPump){intfuelInTank=0;intcurrentPump=startPump;while(true){fuelInTank+=pumps[currentPump].fuel;// Fill her upif(fuelInTank<pumps[currentPump].distanceToNext){// Not enough fuel to reach next pumpreturnfalse;}else{intnextPump=(currentPump+1)%pumps.size();if(nextPump==startPump){returntrue;// Completed loop}else{fuelInTank-=pumps[currentPump].distanceToNext;currentPump=nextPump;// Keep on truckin}}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Truck Tour
You are viewing a single comment's thread. Return to all comments →
I used a
<vector>
rather than a<queue>
, and then just wrapped the index around to make it a circular queue. Still a "queue-based solution" but a bit more efficient than repeatedly pushing and popping elements.