3 Commits b791f939c1 ... 286c947da4

Author SHA1 Message Date
  Zhang Li 286c947da4 feat: min_stack 11 months ago
  Zhang Li db048a6096 feat: find_minimum_in_rotated_array2 11 months ago
  Zhang Li 5e1006d91b feat: find_minimum_in_rotated_array 11 months ago

+ 37 - 0
src/solutions/binary_search/find_minimum_in_rotated_array.rs

@@ -0,0 +1,37 @@
+struct Solution;
+
+impl Solution {
+    pub fn find_min(nums: Vec<i32>) -> i32 {
+        Self::helper(&nums, 0, nums.len() -1)
+    }
+
+    fn helper(nums: &Vec<i32>, s: usize, e: usize) -> i32 {
+        if s == e { return nums[s]; }
+        if s + 1 == e { return nums[s].min(nums[e]); }
+
+        if nums[s] < nums[e] { return nums[s]; }
+
+        let mid = (s + e) / 2;
+        if nums[mid] > nums[e] {
+            return Self::helper(nums, mid, e);
+        }
+
+        if nums[s] > nums[mid] {
+            return Self::helper(nums, s, mid);
+        }
+
+        0
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_find_min() {
+        let nums = vec![3,4,5,1,2];
+        let res = Solution::find_min(nums);
+        assert_eq!(res, 1);
+    }
+}

+ 41 - 0
src/solutions/binary_search/find_minimum_in_rotated_array2.rs

@@ -0,0 +1,41 @@
+struct Solution;
+impl Solution {
+    pub fn find_min(nums: Vec<i32>) -> i32 {
+        Self::helper(&nums, 0, nums.len()-1)
+    }
+
+    fn helper(nums: &Vec<i32>, left: usize, right: usize) -> i32 {
+        if left == right { return nums[left]; }
+        if left + 1 == right { return nums[left].min(nums[right]); }
+        if nums[left] < nums[right] { return nums[left]; }
+        let mid = (left + right) / 2;
+        Self::helper(nums, left, mid).min(Self::helper(nums, mid+1, right))
+    }
+}
+
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_find_min() {
+        let nums = vec![3,4,5,1,2];
+        let res = Solution::find_min(nums);
+        assert_eq!(res, 1);
+    }
+
+    #[test]
+    fn test_find_min2() {
+        let nums = vec![3,3,3,3,3];
+        let res = Solution::find_min(nums);
+        assert_eq!(res, 3);
+    }
+
+    #[test]
+    fn test_find_min3() {
+        let nums = vec![2,2,2,0,1];
+        let res = Solution::find_min(nums);
+        assert_eq!(res, 0);
+    }
+}

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

@@ -6,4 +6,6 @@ mod search_2d_matrix;
 mod search_in_rotated_sorted_array2;
 mod median_of_two_sorted_arrays;
 mod binary_tree_preorder_traversal;
-mod binary_tree_postorder_traversal;
+mod binary_tree_postorder_traversal;
+mod find_minimum_in_rotated_array;
+mod find_minimum_in_rotated_array2;

+ 39 - 0
src/solutions/stack/min_stack.rs

@@ -0,0 +1,39 @@
+use std::cmp;
+
+struct StackElement {
+    val: i32,
+    min: i32,
+}
+
+struct MinStack {
+    stack: Vec<StackElement>,
+    min: i32,
+}
+
+impl MinStack {
+    fn new() -> Self {
+        MinStack {
+            stack: Vec::new(),
+            min: i32::MAX,
+        }
+    }
+
+    fn push(&mut self, val: i32) {
+        let elem = StackElement {val: val, min: self.min};
+        self.stack.push(elem);
+        self.min = cmp::min(self.min, val);
+    }
+
+    fn pop(&mut self) {
+        let value = self.stack.pop().unwrap();
+        self.min = value.min;
+    }
+
+    fn top(&self) -> i32 {
+        self.stack.last().unwrap().val
+    }
+
+    fn get_min(&self) -> i32 {
+        self.min
+    }
+}