Skip to content

Commit 806111f

Browse files
authored
feat: add rust solution to lc problem: No.1458 (#1775)
1 parent af6edcf commit 806111f

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

solution/1400-1499/1458.Max Dot Product of Two Subsequences/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,31 @@ public:
139139
};
140140
```
141141
142+
### **Rust**
143+
144+
```rust
145+
impl Solution {
146+
#[allow(dead_code)]
147+
pub fn max_dot_product(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
148+
let n = nums1.len();
149+
let m = nums2.len();
150+
let mut dp = vec![vec![i32::MIN; m + 1]; n + 1];
151+
152+
// Begin the actual dp process
153+
for i in 1..=n {
154+
for j in 1..=m {
155+
dp[i][j] = std::cmp::max(
156+
std::cmp::max(dp[i - 1][j], dp[i][j - 1]),
157+
std::cmp::max(dp[i - 1][j - 1], 0) + nums1[i - 1] * nums2[j - 1],
158+
);
159+
}
160+
}
161+
162+
dp[n][m]
163+
}
164+
}
165+
```
166+
142167
### **Go**
143168

144169
```go

solution/1400-1499/1458.Max Dot Product of Two Subsequences/README_EN.md

+25
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,31 @@ public:
105105
};
106106
```
107107
108+
### **Rust**
109+
110+
```rust
111+
impl Solution {
112+
#[allow(dead_code)]
113+
pub fn max_dot_product(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
114+
let n = nums1.len();
115+
let m = nums2.len();
116+
let mut dp = vec![vec![i32::MIN; m + 1]; n + 1];
117+
118+
// Begin the actual dp process
119+
for i in 1..=n {
120+
for j in 1..=m {
121+
dp[i][j] = std::cmp::max(
122+
std::cmp::max(dp[i - 1][j], dp[i][j - 1]),
123+
std::cmp::max(dp[i - 1][j - 1], 0) + nums1[i - 1] * nums2[j - 1],
124+
);
125+
}
126+
}
127+
128+
dp[n][m]
129+
}
130+
}
131+
```
132+
108133
### **Go**
109134

110135
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
#[allow(dead_code)]
3+
pub fn max_dot_product(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
4+
let n = nums1.len();
5+
let m = nums2.len();
6+
let mut dp = vec![vec![i32::MIN; m + 1]; n + 1];
7+
8+
// Begin the actual dp process
9+
for i in 1..=n {
10+
for j in 1..=m {
11+
dp[i][j] = std::cmp::max(
12+
std::cmp::max(dp[i - 1][j], dp[i][j - 1]),
13+
std::cmp::max(dp[i - 1][j - 1], 0) + nums1[i - 1] * nums2[j - 1],
14+
);
15+
}
16+
}
17+
18+
dp[n][m]
19+
}
20+
}

0 commit comments

Comments
 (0)