File tree 3 files changed +70
-0
lines changed
solution/1400-1499/1458.Max Dot Product of Two Subsequences
3 files changed +70
-0
lines changed Original file line number Diff line number Diff line change @@ -139,6 +139,31 @@ public:
139
139
};
140
140
```
141
141
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
+
142
167
### ** Go**
143
168
144
169
``` go
Original file line number Diff line number Diff line change @@ -105,6 +105,31 @@ public:
105
105
};
106
106
```
107
107
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
+
108
133
### ** Go**
109
134
110
135
``` go
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments