← Pointer Manipulation

Micro-Drill #148 — Gas station circular

Pointer Manipulation Target: 15s

Track running surplus and total balance. Reset start when tank goes negative.

total = tank = start = 0
for i in range(len(gas)):
    diff = gas[i] - cost[i]
    total += diff
    tank += diff
    if tank < 0:
        start = i + 1
        tank = 0
return start if total >= 0 else -1

Type it from memory. Go.

Practice Problems

Related Coding Drills

← Micro #147 Micro #149 →