3 Commits 6bdc29d871 ... c837c9ab3a

Author SHA1 Message Date
  Zhang Li c837c9ab3a fix: gas_station 1 year ago
  Zhang Li 33b88b4f89 fix: longest_palindromic_substring 1 year ago
  Zhang Li d124b990ca feat: palindrome_partition2 1 year ago

+ 0 - 0
src/solutions/longest_palindromic_substring.rs → src/solutions/array/longest_palindromic_substring.rs


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

@@ -11,4 +11,5 @@ mod merge_sorted_array;
 mod pascal_triangle;
 mod pascal_triangle2;
 mod triangle;
-mod longest_consecutive_sequence;
+mod longest_consecutive_sequence;
+mod longest_palindromic_substring;

+ 94 - 0
src/solutions/bt/gas_station.rs

@@ -0,0 +1,94 @@
+struct Solution;
+
+impl Solution {
+    pub fn can_complete_circuit(gas: Vec<i32>, cost: Vec<i32>) -> i32 {
+        let mut gs = gas.iter().sum::<i32>();
+        let mut cs = cost.iter().sum::<i32>();
+
+        if gs < cs {
+            return -1;
+        }
+
+        let mut c = gas[0] - cost[0];
+
+        let mut m = c;
+        let mut mi = 0;
+
+        for i in 1..gas.len() {
+            c += gas[i] - cost[i];
+
+            if c <= m {
+                m = c;
+                mi = i;
+            }
+        }
+
+        if mi == gas.len() - 1 {
+            0
+        } else {
+            (mi + 1) as i32
+        }
+    }
+    pub fn can_complete_circuit1(gas: Vec<i32>, cost: Vec<i32>) -> i32 {
+        let n = gas.len();
+
+
+        for i in 0..n {
+            if gas[i] < cost[i] { continue; }
+            if Solution::helper(&gas, &cost, n, gas[i], i, (i+1)%n) {
+                return i as i32;
+            }
+        }
+
+        -1
+    }
+
+    fn gas_cost_idx(idx: usize, n: usize) -> (usize, usize){
+        if idx == n {
+            (0, n-1)
+        } else if idx == 0 {
+            (0, n-1)
+        } else {
+            (idx, idx-1)
+        }
+
+    }
+
+    fn helper(gas: &Vec<i32>, cost: &Vec<i32>, n: usize, tank_gas: i32, begin_idx: usize, cur_idx: usize) -> bool {
+
+        let (gas_idx, cost_idx) = Solution::gas_cost_idx(cur_idx, n);
+        if tank_gas - cost[cost_idx] >= 0 && cur_idx == begin_idx {
+            return true;
+        }
+
+        if tank_gas - cost[cost_idx] < 0 {
+            return false;
+        }
+
+        let new_tank_gas = tank_gas - cost[cost_idx] + gas[gas_idx];
+        Solution::helper(gas, cost, n, new_tank_gas, begin_idx, (cur_idx + 1) % n)
+
+
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_can_complete_circuit() {
+        let gas = vec![1,2,3,4,5];
+        let cost = vec![3,4,5,1,2];
+        let res = Solution::can_complete_circuit(gas, cost);
+        assert_eq!(res, 3);
+    }
+
+    #[test]
+    fn test_can_complete_circuit1() {
+        let gas = vec![1,2,3,4,5];
+        let cost = vec![3,4,5,1,2];
+        let res = Solution::can_complete_circuit(gas, cost);
+        assert_eq!(res, 3);
+    }
+}

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

@@ -14,4 +14,5 @@ mod word_search;
 mod generate_parentheses;
 mod subsets2;
 mod restore_ip_addresses;
-mod surrounded_regions;
+mod surrounded_regions;
+mod gas_station;

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

@@ -14,4 +14,5 @@ mod distinct_subsequences;
 mod best_time_to_buy_and_sell_stock;
 mod best_time_to_buy_and_sell_stock2;
 mod best_time_to_buy_and_sell_stock3;
-mod palindrome_partitioning;
+mod palindrome_partitioning;
+mod palindrome_partitioning2;

+ 73 - 0
src/solutions/dp/palindrome_partitioning2.rs

@@ -0,0 +1,73 @@
+struct Solution;
+
+impl Solution {
+    pub fn min_cut(s: String) -> i32 {
+
+        let valid = Solution::valid_palindrome(&s[..]);
+        let s = s.chars().collect::<Vec<char>>();
+
+        let n = s.len();
+        let mut dp = vec![n as i32;n];
+        dp[0] = 0;
+
+        for i in 1..n {
+            for k in 0..=i {
+                if k == 0 {
+                    if valid[k][i] {
+                        dp[i] = dp[i].min(0);
+                    }
+
+                } else {
+                    if valid[k][i] {
+                        dp[i] = dp[i].min(dp[k-1] + 1);
+                    }
+                }
+            }
+        }
+
+        dp[n-1]
+
+    }
+
+
+    fn valid_palindrome(s: &str) ->  Vec<Vec<bool>> {
+        let s = s.chars().collect::<Vec<char>>();
+        let n = s.len();
+        let mut res = vec![vec![false; n]; n];
+
+        for i in 0..n {
+            res[i][i] = true;
+        }
+        for l in 2..=n {
+            for i in 0..(n-l+1) {
+                let j = i + l - 1;
+                if l == 2 {
+                    if s[i] == s[j] {
+                        res[i][j] = true;
+                    }
+
+                } else {
+                    res[i][j] = res[i+1][j-1] && s[i] == s[j]
+                }
+
+            }
+        }
+
+
+
+        res
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_min_cut() {
+        let s = String::from("aab");
+        let res = Solution::min_cut(s);
+        assert_eq!(res, 1);
+
+    }
+}

+ 0 - 1
src/solutions/mod.rs

@@ -15,7 +15,6 @@ mod integer_to_roman;
 mod letter_combinations_of_a_phone_number;
 mod list;
 mod longest_common_prefix;
-mod longest_palindromic_substring;
 mod longest_substring_without_repeating_characters;
 mod move_zeros;
 mod other;