Skip to content

Commit d82bf99

Browse files
authored
feat: add rust solution to lc problem: No.2739 (#1210)
No.2739.Total Distance Traveled
1 parent ad9c6d3 commit d82bf99

File tree

6 files changed

+83
-2
lines changed

6 files changed

+83
-2
lines changed

solution/0000-0099/0018.4Sum/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class Solution {
147147
public:
148148
vector<vector<int>> fourSum(vector<int>& nums, int target) {
149149
int n = nums.size();
150-
vector<vector<int> > ans;
150+
vector<vector<int>> ans;
151151
if (n < 4) {
152152
return ans;
153153
}

solution/0000-0099/0018.4Sum/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class Solution {
129129
public:
130130
vector<vector<int>> fourSum(vector<int>& nums, int target) {
131131
int n = nums.size();
132-
vector<vector<int> > ans;
132+
vector<vector<int>> ans;
133133
if (n < 4) {
134134
return ans;
135135
}

solution/2700-2799/2739.Total Distance Traveled/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,30 @@ func distanceTraveled(mainTank int, additionalTank int) (ans int) {
134134
}
135135
```
136136

137+
### **Rust**
138+
139+
```rust
140+
impl Solution {
141+
pub fn distance_traveled(mut main_tank: i32, mut additional_tank: i32) -> i32 {
142+
let mut cur = 0;
143+
let mut ans = 0;
144+
145+
while main_tank > 0 {
146+
cur += 1;
147+
main_tank -= 1;
148+
ans += 10;
149+
150+
if cur % 5 == 0 && additional_tank > 0 {
151+
additional_tank -= 1;
152+
main_tank += 1;
153+
}
154+
}
155+
156+
ans
157+
}
158+
}
159+
```
160+
137161
### **...**
138162

139163
```

solution/2700-2799/2739.Total Distance Traveled/README_EN.md

+24
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,30 @@ func distanceTraveled(mainTank int, additionalTank int) (ans int) {
121121
}
122122
```
123123

124+
### **Rust**
125+
126+
```rust
127+
impl Solution {
128+
pub fn distance_traveled(mut main_tank: i32, mut additional_tank: i32) -> i32 {
129+
let mut cur = 0;
130+
let mut ans = 0;
131+
132+
while main_tank > 0 {
133+
cur += 1;
134+
main_tank -= 1;
135+
ans += 10;
136+
137+
if cur % 5 == 0 && additional_tank > 0 {
138+
additional_tank -= 1;
139+
main_tank += 1;
140+
}
141+
}
142+
143+
ans
144+
}
145+
}
146+
```
147+
124148
### **...**
125149

126150
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn distance_traveled(mut main_tank: i32, mut additional_tank: i32) -> i32 {
3+
let mut cur = 0;
4+
let mut ans = 0;
5+
6+
while main_tank > 0 {
7+
cur += 1;
8+
main_tank -= 1;
9+
ans += 10;
10+
11+
if cur % 5 == 0 && additional_tank > 0 {
12+
additional_tank -= 1;
13+
main_tank += 1;
14+
}
15+
}
16+
17+
ans
18+
}
19+
}

solution/2700-2799/2742.Painting the Walls/README_EN.md

+14
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@
4242

4343
## Solutions
4444

45+
**Solution 1: Memorization**
46+
47+
We can consider whether each wall is painted by a paid painter or a free painter. Design a function $dfs(i, j)$, which means that from the $i$th wall, and the current remaining free painter working time is $j$, the minimum cost of painting all the remaining walls. Then the answer is $dfs(0, 0)$.
48+
49+
The calculation process of function $dfs(i, j)$ is as follows:
50+
51+
- If $n - i \le j$, it means that there are no more walls than the free painter's working time, so the remaining walls are painted by the free painter, and the cost is $0$;
52+
- If $i \ge n$, return $+\infty$;
53+
- Otherwise, if the $i$th wall is painted by a paid painter, the cost is $cost[i]$, then $dfs(i, j) = dfs(i + 1, j + time[i]) + cost[i]$; if the $i$th wall is painted by a free painter, the cost is $0$, then $dfs(i, j) = dfs(i + 1, j - 1)$.
54+
55+
Note that the parameter $j$ may be less than $0$. Therefore, in the actual coding process, except for the $Python$ language, we add an offset $n$ to $j$ so that the range of $j$ is $[0, 2n]$.
56+
57+
Time complexity $O(n^2)$, space complexity $O(n^2)$. Where $n$ is the length of the array.
58+
4559
<!-- tabs:start -->
4660

4761
### **Python3**

0 commit comments

Comments
 (0)