Watch 10 video solutions for Total Distance Traveled, a easy level problem involving Math, Simulation. This walkthrough by Developer Docs has 805 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
A truck has two fuel tanks. You are given two integers, mainTank representing the fuel present in the main tank in liters and additionalTank representing the fuel present in the additional tank in liters.
The truck has a mileage of 10 km per liter. Whenever 5 liters of fuel get used up in the main tank, if the additional tank has at least 1 liters of fuel, 1 liters of fuel will be transferred from the additional tank to the main tank.
Return the maximum distance which can be traveled.
Note: Injection from the additional tank is not continuous. It happens suddenly and immediately for every 5 liters consumed.
Example 1:
Input: mainTank = 5, additionalTank = 10 Output: 60 Explanation: 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 Explanation: After spending 1 litre of fuel, the main tank becomes empty. Total distance traveled is 10km.
Constraints:
1 <= mainTank, additionalTank <= 100Problem Overview: You are given two integers: mainTank and additionalTank. A truck consumes 1 liter of fuel for every 10 km traveled. Each time the truck uses 5 liters from the main tank, you can transfer 1 liter from the additional tank to the main tank (if available). The goal is to compute the total distance the truck can travel.
Approach 1: Simulation Approach (Time: O(n), Space: O(1))
This method follows the exact process described in the problem using simulation. You repeatedly consume fuel from the main tank while tracking how many liters have been used. After every 5 liters consumed, check whether the additional tank still has fuel. If it does, transfer 1 liter to the main tank and continue driving. The loop continues until the main tank becomes empty. The final distance equals the total liters consumed multiplied by 10. This approach mirrors the real-world process step by step and is useful when verifying the mechanics of the rule or when constraints are small.
Approach 2: Direct Calculation Approach (Time: O(1), Space: O(1))
The fuel transfer rule reveals a mathematical pattern. For every 5 liters consumed from the main tank, you gain 1 extra liter from the additional tank. However, the last few liters may not complete another group of 5, so they do not trigger another transfer. The number of possible transfers is therefore (mainTank - 1) / 4 using integer division. This works because each transfer effectively increases usable fuel before the next group of five is completed. The actual number of transferred liters is limited by additionalTank, so the formula becomes extra = min(additionalTank, (mainTank - 1) // 4). The total usable fuel becomes mainTank + extra, and the final distance is that value multiplied by 10.
This approach relies on recognizing the repeating pattern rather than explicitly simulating each step. It uses simple math and constant-time arithmetic, which makes it significantly cleaner and faster.
Recommended for interviews: The direct calculation approach is usually what interviewers expect. Simulation demonstrates that you understand the mechanics of the rule, but recognizing the mathematical pattern and reducing the solution to O(1) time shows stronger problem-solving and optimization skills.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simulation | O(n) | O(1) | Useful when implementing the process exactly as described or validating the transfer logic step-by-step |
| Direct Calculation (Math Formula) | O(1) | O(1) | Best choice for interviews and production code since the pattern allows constant-time computation |