Skip to content

Commit 05f5809

Browse files
committed
feat: add solutions to lc problems: No.2739~2742
* No.2739.Total Distance Traveled * No.2740.Find the Value of the Partition * No.2741.Special Permutations * No.2742.Painting the Walls
1 parent 3394412 commit 05f5809

30 files changed

+17954
-16363
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# [2739. 总行驶距离](https://leetcode.cn/problems/total-distance-traveled)
2+
3+
[English Version](/solution/2700-2799/2739.Total%20Distance%20Traveled/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>卡车有两个油箱。给你两个整数,<code>mainTank</code> 表示主油箱中的燃料(以升为单位),<code>additionalTank</code> 表示副油箱中的燃料(以升为单位)。</p>
10+
11+
<p>该卡车每耗费 <code>1</code> 升燃料都可以行驶 <code>10</code> km。每当主油箱使用了 <code>5</code> 升燃料时,如果副油箱至少有 <code>1</code> 升燃料,则会将 <code>1</code> 升燃料从副油箱转移到主油箱。</p>
12+
13+
<p>返回卡车可以行驶的最大距离。</p>
14+
15+
<p>注意:从副油箱向主油箱注入燃料不是连续行为。这一事件会在每消耗 <code>5</code> 升燃料时突然且立即发生。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<pre><strong>输入:</strong>mainTank = 5, additionalTank = 10
22+
<strong>输出:</strong>60
23+
<strong>解释:</strong>
24+
在用掉 5 升燃料后,主油箱中燃料还剩下 (5 - 5 + 1) = 1 升,行驶距离为 50km 。
25+
在用掉剩下的 1 升燃料后,没有新的燃料注入到主油箱中,主油箱变为空。
26+
总行驶距离为 60km 。
27+
</pre>
28+
29+
<p><strong>示例 2:</strong></p>
30+
31+
<pre><strong>输入:</strong>mainTank = 1, additionalTank = 2
32+
<strong>输出:</strong>10
33+
<strong>解释:</strong>
34+
在用掉 1 升燃料后,主油箱变为空。
35+
总行驶距离为 10km 。
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
40+
<p><strong>提示:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= mainTank, additionalTank &lt;= 100</code></li>
44+
</ul>
45+
46+
## 解法
47+
48+
<!-- 这里可写通用的实现逻辑 -->
49+
50+
**方法一:模拟**
51+
52+
我们可以模拟卡车的行驶过程,每次消耗 $1$ 升主油箱中的燃料,行驶 $10$ 公里。每当主油箱中的燃料消耗 $5$ 升时,如果副油箱中有燃料,则将副油箱中的 $1$ 升燃料转移到主油箱中。一直模拟到主油箱中的燃料消耗完为止。
53+
54+
时间复杂度 $O(n + m)$,其中 $n$ 和 $m$ 分别是主油箱和副油箱中的燃料数量。空间复杂度 $O(1)$。
55+
56+
<!-- tabs:start -->
57+
58+
### **Python3**
59+
60+
<!-- 这里可写当前语言的特殊实现逻辑 -->
61+
62+
```python
63+
class Solution:
64+
def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:
65+
ans = cur = 0
66+
while mainTank:
67+
cur += 1
68+
ans += 10
69+
mainTank -= 1
70+
if cur % 5 == 0 and additionalTank:
71+
additionalTank -= 1
72+
mainTank += 1
73+
return ans
74+
```
75+
76+
### **Java**
77+
78+
<!-- 这里可写当前语言的特殊实现逻辑 -->
79+
80+
```java
81+
class Solution {
82+
public int distanceTraveled(int mainTank, int additionalTank) {
83+
int ans = 0, cur = 0;
84+
while (mainTank > 0) {
85+
cur++;
86+
ans += 10;
87+
mainTank--;
88+
if (cur % 5 == 0 && additionalTank > 0) {
89+
additionalTank--;
90+
mainTank++;
91+
}
92+
}
93+
return ans;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int distanceTraveled(int mainTank, int additionalTank) {
104+
int ans = 0, cur = 0;
105+
while (mainTank > 0) {
106+
cur++;
107+
ans += 10;
108+
mainTank--;
109+
if (cur % 5 == 0 && additionalTank > 0) {
110+
additionalTank--;
111+
mainTank++;
112+
}
113+
}
114+
return ans;
115+
}
116+
};
117+
```
118+
119+
### **Go**
120+
121+
```go
122+
func distanceTraveled(mainTank int, additionalTank int) (ans int) {
123+
cur := 0
124+
for mainTank > 0 {
125+
cur++
126+
ans += 10
127+
mainTank--
128+
if cur%5 == 0 && additionalTank > 0 {
129+
additionalTank--
130+
mainTank++
131+
}
132+
}
133+
return
134+
}
135+
```
136+
137+
### **...**
138+
139+
```
140+
141+
```
142+
143+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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&nbsp;used up in the main tank,&nbsp;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>&nbsp;</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>&nbsp;</p>
39+
<p><strong>Constraints:</strong></p>
40+
41+
<ul>
42+
<li><code>1 &lt;= mainTank, additionalTank &lt;= 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 -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int distanceTraveled(int mainTank, int additionalTank) {
4+
int ans = 0, cur = 0;
5+
while (mainTank > 0) {
6+
cur++;
7+
ans += 10;
8+
mainTank--;
9+
if (cur % 5 == 0 && additionalTank > 0) {
10+
additionalTank--;
11+
mainTank++;
12+
}
13+
}
14+
return ans;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func distanceTraveled(mainTank int, additionalTank int) (ans int) {
2+
cur := 0
3+
for mainTank > 0 {
4+
cur++
5+
ans += 10
6+
mainTank--
7+
if cur%5 == 0 && additionalTank > 0 {
8+
additionalTank--
9+
mainTank++
10+
}
11+
}
12+
return
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int distanceTraveled(int mainTank, int additionalTank) {
3+
int ans = 0, cur = 0;
4+
while (mainTank > 0) {
5+
cur++;
6+
ans += 10;
7+
mainTank--;
8+
if (cur % 5 == 0 && additionalTank > 0) {
9+
additionalTank--;
10+
mainTank++;
11+
}
12+
}
13+
return ans;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def distanceTraveled(self, mainTank: int, additionalTank: int) -> int:
3+
ans = cur = 0
4+
while mainTank:
5+
cur += 1
6+
ans += 10
7+
mainTank -= 1
8+
if cur % 5 == 0 and additionalTank:
9+
additionalTank -= 1
10+
mainTank += 1
11+
return ans

0 commit comments

Comments
 (0)