2 Commits c2b70f1840 ... 5fa363f868

Author SHA1 Message Date
  Zhang Li 5fa363f868 feat: dungeon_game 11 months ago
  Zhang Li 1b384ee771 feat: factorial_trailing_zeroes 11 months ago

+ 33 - 0
src/solutions/array/majority_element.rs

@@ -0,0 +1,33 @@
+struct Solution;
+
+
+impl Solution {
+    pub fn majority_element(nums: Vec<i32>) -> i32 {
+        let mut candidate = nums[0];
+        let mut count = 0;
+        for n in nums {
+            if count == 0 {
+                candidate = n;
+            }
+            if n == candidate {
+                count += 1;
+            } else {
+                count -= 1;
+            }
+        }
+
+        candidate
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_majority_element() {
+        let nums = vec![2,2,1,1,1,2,2];
+        let res = Solution::majority_element(nums);
+        assert_eq!(res, 2);
+    }
+}

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

@@ -16,3 +16,4 @@ mod longest_palindromic_substring;
 mod single_number;
 mod single_number2;
 mod maximum_gap;
+mod majority_element;

+ 32 - 0
src/solutions/dp/dungeon_game.rs

@@ -0,0 +1,32 @@
+struct Solution;
+
+impl Solution {
+    pub fn calculate_minimum_hp(dungeon: Vec<Vec<i32>>) -> i32 {
+        let rows = dungeon.len();
+        let cols = dungeon[0].len();
+        let mut dp = vec![vec![i32::MAX; cols+1]; rows+1];
+        dp[rows-1][cols] = 1;
+        dp[rows][cols-1] = 1;
+        for i in (0..rows).rev() {
+            for j in (0..cols).rev() {
+                dp[i][j] = (dp[i][j+1].min(dp[i+1][j]) - dungeon[i][j]).max(1);
+            }
+
+        }
+
+        dp[0][0]
+
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_calculate_minimum_hp() {
+        let dungeon = vec![vec![-2,-3,3],vec![-5,-10,1],vec![10,30,-5]];
+        let res = Solution::calculate_minimum_hp(dungeon);
+        assert_eq!(res, 7);
+    }
+}

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

@@ -19,4 +19,5 @@ mod palindrome_partitioning2;
 mod candy;
 mod word_break;
 mod word_break2;
-mod maximum_product_subarray;
+mod maximum_product_subarray;
+mod dungeon_game;

+ 27 - 0
src/solutions/other/factorial_trailing_zeroes.rs

@@ -0,0 +1,27 @@
+struct Solution;
+impl Solution {
+    pub fn trailing_zeroes(n: i32) -> i32 {
+        n / 5 + n / 25 + n / 125 + n / 625 + n / 3125
+
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_trailing_zeroes() {
+        let n = 10;
+        let res = Solution::trailing_zeroes(n);
+        assert_eq!(res, 2);
+    }
+
+    #[test]
+    fn test_trailing_zeroes2() {
+        let n = 25;
+        let res = Solution::trailing_zeroes(n);
+        assert_eq!(res, 6);
+    }
+
+}

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

@@ -8,4 +8,5 @@ mod spiral_matrix_2;
 mod valid_number;
 mod set_matrix_zero;
 mod gray_code;
-mod max_points_on_a_line;
+mod max_points_on_a_line;
+mod factorial_trailing_zeroes;

+ 35 - 0
src/solutions/str/excel_sheet_column_number.rs

@@ -0,0 +1,35 @@
+struct Solution;
+
+impl Solution {
+    pub fn title_to_number(column_title: String) -> i32 {
+        let v = column_title.chars().collect::<Vec<char>>();
+        let mut res = 0;
+        for (i, &c) in v.iter().rev().enumerate() {
+            res += 26_i32.pow(i as u32) * Self::c_to_i(c);
+        }
+
+        res
+    }
+
+    fn c_to_i(c: char) -> i32 {
+        (c as u8 - 'A' as u8) as i32 + 1
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_title_to_number() {
+        let column_title = String::from("AB");
+        let res = Solution::title_to_number(column_title);
+        assert_eq!(res, 28);
+    }
+
+    #[test]
+    fn test_c_to_i() {
+        let res = Solution::c_to_i('B');
+        assert_eq!(res, 2);
+    }
+}

+ 51 - 0
src/solutions/str/excel_sheet_column_title.rs

@@ -0,0 +1,51 @@
+struct Solution;
+
+impl Solution {
+    pub fn convert_to_title(column_number: i32) -> String {
+        let mut v = vec![];
+        let mut residual = column_number;
+        while residual != 0 {
+            let  tmp = if  residual % 26 == 0 { 26 } else { residual % 26 };
+            v.push(tmp);
+
+            residual -= tmp;
+            residual /= 26;
+        }
+        // println!("v: {:?}", v);
+        v.iter().rev().map(|&x| Self::i_to_a(x)).collect()
+
+    }
+
+    fn i_to_a(num: i32) -> char {
+        ('A' as u8 + num as u8 - 1) as char
+    }
+}
+
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_convert_to_title() {
+        let column_number = 701;
+        let res =Solution::convert_to_title(column_number);
+        assert_eq!(res, String::from("ZY"));
+    }
+
+    #[test]
+    fn test_convert_to_title2() {
+        let column_number = 28;
+        let res =Solution::convert_to_title(column_number);
+        assert_eq!(res, String::from("AB"));
+    }
+
+
+
+    #[test]
+    fn test_i_to_a() {
+        let num = 1;
+        let res = Solution::i_to_a(num);
+        assert_eq!(res, 'A');
+    }
+}

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

@@ -4,4 +4,6 @@ mod minimum_window_substring;
 mod string_to_integer;
 mod valid_palindrome;
 mod reverse_words_in_a_string;
-mod compare_version_numbers;
+mod compare_version_numbers;
+mod excel_sheet_column_title;
+mod excel_sheet_column_number;