[LeetCode] Total Distance Traveled

2739. Total Distance Traveled

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int distanceTraveled(int mainTank, int additionalTank) {
int use = 0, res = 0;
while(mainTank) {
mainTank -= 1;
use += 1;
if(use % 5 == 0) {
if(additionalTank) mainTank += 1, additionalTank -= 1;
}
res += 10;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/06/18/PS/LeetCode/total-distance-traveled/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.