From 9a94e1f5feff6ad700d629302f6568460b21ed3a Mon Sep 17 00:00:00 2001 From: Luca Lombardo Date: Fri, 29 Sep 2023 18:45:49 +0200 Subject: [PATCH] new problem solved --- .../Cargo.toml | 8 ++++ .../README.md | 41 +++++++++++++++++++ .../src/main.rs | 35 ++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 2023_09_25/find-minimum-in-rotated-sorted-array/Cargo.toml create mode 100644 2023_09_25/find-minimum-in-rotated-sorted-array/README.md create mode 100644 2023_09_25/find-minimum-in-rotated-sorted-array/src/main.rs diff --git a/2023_09_25/find-minimum-in-rotated-sorted-array/Cargo.toml b/2023_09_25/find-minimum-in-rotated-sorted-array/Cargo.toml new file mode 100644 index 0000000..e7fec15 --- /dev/null +++ b/2023_09_25/find-minimum-in-rotated-sorted-array/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "find-minimum-in-rotated-sorted-array" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2023_09_25/find-minimum-in-rotated-sorted-array/README.md b/2023_09_25/find-minimum-in-rotated-sorted-array/README.md new file mode 100644 index 0000000..4868336 --- /dev/null +++ b/2023_09_25/find-minimum-in-rotated-sorted-array/README.md @@ -0,0 +1,41 @@ +> 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 + +```rust +fn findMin(nums: Vec) -> 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 + +![](https://i.imgur.com/JHStQsr.png) diff --git a/2023_09_25/find-minimum-in-rotated-sorted-array/src/main.rs b/2023_09_25/find-minimum-in-rotated-sorted-array/src/main.rs new file mode 100644 index 0000000..51ee204 --- /dev/null +++ b/2023_09_25/find-minimum-in-rotated-sorted-array/src/main.rs @@ -0,0 +1,35 @@ +fn findMin(nums: Vec) -> i32 { + let mut res = nums[0] as i32; + // The left and right pointers of the binary search + let (mut l, mut r) = (0, nums.len() - 1); + + // Binary search + while l <= r { + // If the left value is smaller than the right value, + // then the left value is the smallest + if nums[l] <= nums[r] { + res = res.min(nums[l]); + break; + } + // The middle value + let mid = l + (r - l) / 2; + // Update the smallest value + res = res.min(nums[mid]); + + // If the left value is smaller than the middle value, + // then the smallest value is on the right side + if nums[l] <= nums[mid] { + l = mid + 1; + } else { + // Otherwise, the smallest value is on the left side + r = mid - 1; + } + } + return res as i32; +} + +fn main() { + let nums = vec![3, 4, 5, 1, 2]; + let res = findMin(nums); + println!("{}", res); +}