2 Commits ae55c70fc0 ... b791f939c1

Author SHA1 Message Date
  Zhang Li b791f939c1 feat: maximum_product_subarray 1 year ago
  Zhang Li 3e470dc9f6 feat: reverse_words_in_a_string 1 year ago

+ 81 - 0
src/solutions/dp/maximum_product_subarray.rs

@@ -0,0 +1,81 @@
+struct Solution;
+
+impl Solution {
+    pub fn max_product1(nums: Vec<i32>) -> i32 {
+        let n = nums.len();
+        let mut dp1 = vec![0;n];
+        let mut dp2 = vec![0;n];
+        let mut ans = nums[0];
+        dp1[0] = nums[0];
+        dp2[0] = nums[0];
+        for i in 1..n {
+            let value1 = dp1[i-1] * nums[i];
+            let value2 = dp2[i-1] * nums[i];
+
+            dp1[i] = value1.max(value2).max(nums[i]);
+            dp2[i] = value1.min(value2).min(nums[i]);
+
+            ans = ans.max(dp1[i]);
+
+        }
+        // println!("dp: {:?}", dp);
+
+        ans
+
+    }
+
+    pub fn max_product(nums: Vec<i32>) -> i32 {
+        let n = nums.len();
+        let mut dp1 = nums[0];
+        let mut dp2 = nums[0];
+        let mut ans = nums[0];
+
+        for i in 1..n {
+            let value1 = dp1 * nums[i];
+            let value2 = dp2 * nums[i];
+
+            dp1 = value1.max(value2).max(nums[i]);
+            dp2 = value1.min(value2).min(nums[i]);
+
+            ans = ans.max(dp1);
+
+        }
+        // println!("dp: {:?}", dp);
+
+        ans
+
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_max_product() {
+        let nums = vec![2,3,-2,-1,4];
+        let res = Solution::max_product1(nums);
+        assert_eq!(res, 48);
+    }
+
+    #[test]
+    fn test_max_product1() {
+        let nums = vec![2,3,-2,4];
+        let res = Solution::max_product(nums);
+        assert_eq!(res, 6);
+    }
+
+    #[test]
+    fn test_max_product2() {
+        let nums = vec![-2,3,-4];
+        let res = Solution::max_product(nums);
+        assert_eq!(res, 24);
+    }
+
+    #[test]
+    fn test_max_product3() {
+        let nums = vec![2,-5,-2, -4, 3];
+        let res = Solution::max_product(nums);
+        assert_eq!(res, 24);
+    }
+}

+ 2 - 1
src/solutions/dp/mod.rs

@@ -18,4 +18,5 @@ mod palindrome_partitioning;
 mod palindrome_partitioning2;
 mod candy;
 mod word_break;
-mod word_break2;
+mod word_break2;
+mod maximum_product_subarray;

+ 2 - 1
src/solutions/str/mod.rs

@@ -2,4 +2,5 @@ mod multiply_strings;
 mod strstr;
 mod minimum_window_substring;
 mod string_to_integer;
-mod valid_palindrome;
+mod valid_palindrome;
+mod reverse_words_in_a_string;

+ 21 - 0
src/solutions/str/reverse_words_in_a_string.rs

@@ -0,0 +1,21 @@
+struct Solution;
+
+impl Solution {
+    pub fn reverse_words(s: String) -> String {
+        s.split_whitespace().rev().collect::<Vec<&str>>().join(" ")
+
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_reverse_words() {
+        let s = String::from("a good   example");
+
+        let res = Solution::reverse_words(s);
+        assert_eq!(res, String::from( "example good a"));
+    }
+}