Leetcode 2739: Total Distance Traveled

grid47
grid47
Exploring patterns and algorithms
Feb 7, 2024 5 min read

A truck has two fuel tanks, one main tank and one additional tank. The truck’s mileage is 10 km per liter, and fuel from the additional tank can be injected into the main tank after every 5 liters of fuel consumed. Your task is to calculate the maximum distance the truck can travel, considering both the main tank and the additional tank.
Problem
Approach
Steps
Complexity
Input: The input consists of two integers: `mainTank` and `additionalTank`, representing the fuel available in the main tank and the additional tank in liters.
Example: mainTank = 8, additionalTank = 12
Constraints:
• 1 <= mainTank <= 100
• 1 <= additionalTank <= 100
Output: Return the maximum distance the truck can travel.
Example: Output: 80
Constraints:
Goal: Calculate the total distance the truck can travel by consuming fuel from the main tank and transferring fuel from the additional tank when necessary.
Steps:
• While there is fuel in the main tank, calculate the distance that can be traveled with the current fuel.
• For every 5 liters consumed, transfer 1 liter from the additional tank to the main tank if possible.
• Repeat until the main tank is empty.
Goal: The problem guarantees that both `mainTank` and `additionalTank` will contain at least 1 liter of fuel and no more than 100 liters.
Steps:
• 1 <= mainTank <= 100
• 1 <= additionalTank <= 100
Assumptions:
• Fuel injection only happens after 5 liters are consumed from the main tank.
• The truck’s mileage is always 10 km per liter of fuel.
Input: mainTank = 8, additionalTank = 12
Explanation: Initially, the truck starts with 8 liters in the main tank, allowing it to travel 80 km. After consuming 5 liters, the additional tank injects 1 liter into the main tank. This continues until the main tank is empty.

Input: mainTank = 3, additionalTank = 6
Explanation: The truck starts with 3 liters in the main tank, which allows it to travel 30 km. No fuel is injected from the additional tank because fuel transfer only happens after consuming 5 liters.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus