|
| 1 | +# [2739. Total Distance Traveled](https://leetcode.com/problems/total-distance-traveled) |
| 2 | + |
| 3 | +[中文文档](/solution/2700-2799/2739.Total%20Distance%20Traveled/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p>A truck has two fuel tanks. You are given two integers, <code>mainTank</code> representing the fuel present in the main tank in liters and <code>additionalTank</code> representing the fuel present in the additional tank in liters.</p> |
| 8 | + |
| 9 | +<p>The truck has a mileage of <code>10</code> km per liter. Whenever <code>5</code> liters of fuel get used up in the main tank, if the additional tank has at least <code>1</code> liters of fuel, <code>1</code> liters of fuel will be transferred from the additional tank to the main tank.</p> |
| 10 | + |
| 11 | +<p>Return <em>the maximum distance which can be traveled.</em></p> |
| 12 | + |
| 13 | +<p><strong>Note: </strong>Injection from the additional tank is not continuous. It happens suddenly and immediately for every 5 liters consumed.</p> |
| 14 | + |
| 15 | +<p> </p> |
| 16 | +<p><strong class="example">Example 1:</strong></p> |
| 17 | + |
| 18 | +<pre> |
| 19 | +<strong>Input:</strong> mainTank = 5, additionalTank = 10 |
| 20 | +<strong>Output:</strong> 60 |
| 21 | +<strong>Explanation:</strong> |
| 22 | +After spending 5 litre of fuel, fuel remaining is (5 - 5 + 1) = 1 litre and distance traveled is 50km. |
| 23 | +After spending another 1 litre of fuel, no fuel gets injected in the main tank and the main tank becomes empty. |
| 24 | +Total distance traveled is 60km. |
| 25 | +</pre> |
| 26 | + |
| 27 | +<p><strong class="example">Example 2:</strong></p> |
| 28 | + |
| 29 | +<pre> |
| 30 | +<strong>Input:</strong> mainTank = 1, additionalTank = 2 |
| 31 | +<strong>Output:</strong> 10 |
| 32 | +<strong>Explanation:</strong> |
| 33 | +After spending 1 litre of fuel, the main tank becomes empty. |
| 34 | +Total distance traveled is 10km. |
| 35 | + |
| 36 | +</pre> |
| 37 | + |
| 38 | +<p> </p> |
| 39 | +<p><strong>Constraints:</strong></p> |
| 40 | + |
| 41 | +<ul> |
| 42 | + <li><code>1 <= mainTank, additionalTank <= 100</code></li> |
| 43 | +</ul> |
| 44 | + |
| 45 | +## Solutions |
| 46 | + |
| 47 | +<!-- tabs:start --> |
| 48 | + |
| 49 | +### **Python3** |
| 50 | + |
| 51 | +```python |
| 52 | +class Solution: |
| 53 | + def distanceTraveled(self, mainTank: int, additionalTank: int) -> int: |
| 54 | + ans = cur = 0 |
| 55 | + while mainTank: |
| 56 | + cur += 1 |
| 57 | + ans += 10 |
| 58 | + mainTank -= 1 |
| 59 | + if cur % 5 == 0 and additionalTank: |
| 60 | + additionalTank -= 1 |
| 61 | + mainTank += 1 |
| 62 | + return ans |
| 63 | +``` |
| 64 | + |
| 65 | +### **Java** |
| 66 | + |
| 67 | +```java |
| 68 | +class Solution { |
| 69 | + public int distanceTraveled(int mainTank, int additionalTank) { |
| 70 | + int ans = 0, cur = 0; |
| 71 | + while (mainTank > 0) { |
| 72 | + cur++; |
| 73 | + ans += 10; |
| 74 | + mainTank--; |
| 75 | + if (cur % 5 == 0 && additionalTank > 0) { |
| 76 | + additionalTank--; |
| 77 | + mainTank++; |
| 78 | + } |
| 79 | + } |
| 80 | + return ans; |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### **C++** |
| 86 | + |
| 87 | +```cpp |
| 88 | +class Solution { |
| 89 | +public: |
| 90 | + int distanceTraveled(int mainTank, int additionalTank) { |
| 91 | + int ans = 0, cur = 0; |
| 92 | + while (mainTank > 0) { |
| 93 | + cur++; |
| 94 | + ans += 10; |
| 95 | + mainTank--; |
| 96 | + if (cur % 5 == 0 && additionalTank > 0) { |
| 97 | + additionalTank--; |
| 98 | + mainTank++; |
| 99 | + } |
| 100 | + } |
| 101 | + return ans; |
| 102 | + } |
| 103 | +}; |
| 104 | +``` |
| 105 | +
|
| 106 | +### **Go** |
| 107 | +
|
| 108 | +```go |
| 109 | +func distanceTraveled(mainTank int, additionalTank int) (ans int) { |
| 110 | + cur := 0 |
| 111 | + for mainTank > 0 { |
| 112 | + cur++ |
| 113 | + ans += 10 |
| 114 | + mainTank-- |
| 115 | + if cur%5 == 0 && additionalTank > 0 { |
| 116 | + additionalTank-- |
| 117 | + mainTank++ |
| 118 | + } |
| 119 | + } |
| 120 | + return |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +### **...** |
| 125 | + |
| 126 | +``` |
| 127 | +
|
| 128 | +``` |
| 129 | + |
| 130 | +<!-- tabs:end --> |
0 commit comments