diff --git a/solution/1400-1499/1458.Max Dot Product of Two Subsequences/README.md b/solution/1400-1499/1458.Max Dot Product of Two Subsequences/README.md index c468ed294e1aa..8fa152ea038fd 100644 --- a/solution/1400-1499/1458.Max Dot Product of Two Subsequences/README.md +++ b/solution/1400-1499/1458.Max Dot Product of Two Subsequences/README.md @@ -139,6 +139,31 @@ public: }; ``` +### **Rust** + +```rust +impl Solution { + #[allow(dead_code)] + pub fn max_dot_product(nums1: Vec, nums2: Vec) -> i32 { + let n = nums1.len(); + let m = nums2.len(); + let mut dp = vec![vec![i32::MIN; m + 1]; n + 1]; + + // Begin the actual dp process + for i in 1..=n { + for j in 1..=m { + dp[i][j] = std::cmp::max( + std::cmp::max(dp[i - 1][j], dp[i][j - 1]), + std::cmp::max(dp[i - 1][j - 1], 0) + nums1[i - 1] * nums2[j - 1], + ); + } + } + + dp[n][m] + } +} +``` + ### **Go** ```go diff --git a/solution/1400-1499/1458.Max Dot Product of Two Subsequences/README_EN.md b/solution/1400-1499/1458.Max Dot Product of Two Subsequences/README_EN.md index e5c4de440d482..5854263bd0125 100644 --- a/solution/1400-1499/1458.Max Dot Product of Two Subsequences/README_EN.md +++ b/solution/1400-1499/1458.Max Dot Product of Two Subsequences/README_EN.md @@ -105,6 +105,31 @@ public: }; ``` +### **Rust** + +```rust +impl Solution { + #[allow(dead_code)] + pub fn max_dot_product(nums1: Vec, nums2: Vec) -> i32 { + let n = nums1.len(); + let m = nums2.len(); + let mut dp = vec![vec![i32::MIN; m + 1]; n + 1]; + + // Begin the actual dp process + for i in 1..=n { + for j in 1..=m { + dp[i][j] = std::cmp::max( + std::cmp::max(dp[i - 1][j], dp[i][j - 1]), + std::cmp::max(dp[i - 1][j - 1], 0) + nums1[i - 1] * nums2[j - 1], + ); + } + } + + dp[n][m] + } +} +``` + ### **Go** ```go diff --git a/solution/1400-1499/1458.Max Dot Product of Two Subsequences/Solution.rs b/solution/1400-1499/1458.Max Dot Product of Two Subsequences/Solution.rs new file mode 100644 index 0000000000000..f165ff211442a --- /dev/null +++ b/solution/1400-1499/1458.Max Dot Product of Two Subsequences/Solution.rs @@ -0,0 +1,20 @@ +impl Solution { + #[allow(dead_code)] + pub fn max_dot_product(nums1: Vec, nums2: Vec) -> i32 { + let n = nums1.len(); + let m = nums2.len(); + let mut dp = vec![vec![i32::MIN; m + 1]; n + 1]; + + // Begin the actual dp process + for i in 1..=n { + for j in 1..=m { + dp[i][j] = std::cmp::max( + std::cmp::max(dp[i - 1][j], dp[i][j - 1]), + std::cmp::max(dp[i - 1][j - 1], 0) + nums1[i - 1] * nums2[j - 1], + ); + } + } + + dp[n][m] + } +} \ No newline at end of file