• + 1 comment

    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.

    struct Pump {
        int fuel;
        int distanceToNext;
        Pump(int f,int d) : fuel(f), distanceToNext(d) {}
    };
    
    bool getTruckin (vector<Pump>& pumps, int startPump) {
        int fuelInTank = 0;
        int currentPump = startPump;
        
        while (true) {
            fuelInTank += pumps[currentPump].fuel; // Fill her up
            
            if (fuelInTank < pumps[currentPump].distanceToNext) {
                // Not enough fuel to reach next pump
                return false; 
            } else {
                int nextPump = (currentPump + 1) % pumps.size();
                if (nextPump == startPump) {
                    return true; // Completed loop
                } else {
                    fuelInTank -= pumps[currentPump].distanceToNext;
                    currentPump = nextPump; // Keep on truckin
                }
            }
        }
    }