|
@@ -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);
|
|
|
+ }
|
|
|
+}
|