Leetcode Problem 2739. Total Distance Traveled

2739. Total Distance Traveled

Leetcode Solutions

Iterative Fuel Consumption

  1. Initialize a variable distance to 0 to keep track of the total distance traveled.
  2. Loop while there is fuel in the main tank. a. If the main tank has 5 or more liters, consume 5 liters and increase the distance by 50 km. b. If the additional tank has at least 1 liter, transfer 1 liter to the main tank. c. If the main tank has less than 5 liters, consume the remaining fuel and add the corresponding distance. d. End the loop when the main tank is empty.
  3. Return the total distance traveled.
UML Thumbnail

Recursive Fuel Consumption

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...