LeetCode Problem Workspace

Total Distance Traveled

Calculate the maximum distance a truck can travel using main and additional fuel tanks with controlled transfers.

category

2

Topics

code_blocks

6

Code langs

hub

3

Related

Practice Focus

Easy · Math plus Simulation

bolt

Answer-first summary

Calculate the maximum distance a truck can travel using main and additional fuel tanks with controlled transfers.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Math plus Simulation

Try AiBox Copilotarrow_forward

Start by simulating fuel consumption with the main tank and transferring fuel from the additional tank whenever 5 liters are spent. Track distance in increments of 10 km per liter. This problem tests careful math and simulation handling, especially avoiding fractional miscalculations during fuel transfer steps.

Problem Statement

You are given a truck with two fuel tanks: a main tank and an additional tank. Each tank has a certain amount of fuel in liters. The truck drives 10 kilometers per liter. Every time the main tank uses 5 liters, 1 liter of fuel is transferred from the additional tank to the main tank if available.

Given the amount of fuel in the main tank and additional tank, calculate the total maximum distance the truck can travel before both tanks run out. Implement a solution that accounts for incremental fuel transfers from the additional tank when thresholds are reached.

Examples

Example 1

Input: mainTank = 5, additionalTank = 10

Output: 60

After spending 5 litre of fuel, fuel remaining is (5 - 5 + 1) = 1 litre and distance traveled is 50km. After spending another 1 litre of fuel, no fuel gets injected in the main tank and the main tank becomes empty. Total distance traveled is 60km.

Example 2

Input: mainTank = 1, additionalTank = 2

Output: 10

After spending 1 litre of fuel, the main tank becomes empty. Total distance traveled is 10km.

Constraints

  • 1 <= mainTank, additionalTank <= 100

Solution Approach

Simulate Fuel Consumption

Iteratively reduce the main tank by 1 liter per 10 km traveled. After every 5 liters consumed, check if additional tank has at least 1 liter to transfer. Continue until main tank is empty and no additional fuel remains.

Track Distance Stepwise

Increment total distance by 10 km for each liter consumed from the main tank. This ensures simulation matches the truck's mileage and accurately accounts for transferred fuel increments from the additional tank.

Avoid Decimal Precision Errors

All calculations should use integer arithmetic. Avoid fractions or floating-point division during transfer checks to prevent off-by-one distance errors in the simulation.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(mainTank + additionalTank) since each liter may trigger a transfer check. Space complexity is O(1) because only counters for distance and remaining fuel are needed.

What Interviewers Usually Probe

  • Asks about simulating incremental fuel consumption and transfers.
  • Questions handling of integer vs decimal calculations in mileage.
  • Tests understanding of threshold-based triggers for additional tank usage.

Common Pitfalls or Variants

Common pitfalls

  • Incorrectly transferring fuel before every 5 liters are spent.
  • Using decimal division and losing precision in distance calculation.
  • Not stopping simulation when both tanks are empty.

Follow-up variants

  • Change the mileage rate to a non-integer value and test integer-only simulation.
  • Allow multiple additional tanks and require sequential transfers.
  • Adjust the transfer rate to more than 1 liter and verify correct distance computation.

FAQ

What is the main pattern in Total Distance Traveled?

The problem combines math and simulation: track fuel usage and transfer rules stepwise to compute total distance.

How do I handle fuel transfers efficiently?

Check the main tank consumption every 5 liters and transfer exactly 1 liter from the additional tank if available, using integer arithmetic.

Can I use floating-point calculations?

No, using decimals can lead to precision errors. Always compute in integer units of liters and distance.

What if the additional tank runs out before the main tank?

Continue consuming the main tank normally. Only transfer when additional fuel exists.

Is there a simple formula for maximum distance?

No exact formula applies because fuel transfers depend on discrete consumption thresholds; simulation is required.

terminal

Solution

Solution 1: Simulation

We can simulate the process of the truck's movement. Each time, it consumes 1 liter of fuel from the main fuel tank and travels 10 kilometers. Whenever the fuel in the main fuel tank is consumed by 5 liters, if there is fuel in the auxiliary fuel tank, 1 liter of fuel is transferred from the auxiliary fuel tank to the main fuel tank. The simulation continues until the fuel in the main fuel tank is exhausted.

1
2
3
4
5
6
7
8
9
10
11
class Solution:
    def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:
        ans = cur = 0
        while mainTank:
            cur += 1
            ans += 10
            mainTank -= 1
            if cur % 5 == 0 and additionalTank:
                additionalTank -= 1
                mainTank += 1
        return ans
Total Distance Traveled Solution: Math plus Simulation | LeetCode #2739 Easy