You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Luca Lombardo 9a94e1f5fe new problem solved 1 year ago
..
src new problem solved 1 year ago
Cargo.toml new problem solved 1 year ago
README.md new problem solved 1 year ago

README.md

Problem: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/submissions/

Comments on the solution

We first initialize the minimum element as the first element of the array. Then we use binary search to find the minimum element. We compare the first element with the last element to check if the array is rotated or not. If the array is not rotated, the first element is the minimum element. If the array is rotated, we use binary search to find the minimum element. We compare the middle element with the first and last elements to determine which half of the array to search. We continue this process until we find the minimum element.

Complexity

  • Time Complexity: O(log(n)). Just binary search
  • Space Complexity: O(1). No extra space is used.

Rust solution

fn findMin(nums: Vec<i32>) -> i32 {
    let mut res = nums[0] as i32;
    let (mut l, mut r) = (0, nums.len() - 1);

    while l <= r {
        if nums[l] <= nums[r] {
            res = res.min(nums[l]);
            break;
        }
        let mid = l + (r - l) / 2;
        res = res.min(nums[mid]);

        if nums[l] <= nums[mid] {
            l = mid + 1;
        } else {
            r = mid - 1;
        }
    }
    res
}

Leetcode result