Skip to content

Commit 158ebde

Browse files
authored
Merge pull request #19 from xchux/feature/153-find-minimum-in-rotated-sorted-array
✨ (Solution): Problem 153. Find Minimum in Rotated Sorted Array
2 parents 48a4d97 + ab040ac commit 158ebde

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
comments: true
3+
difficulty: medium
4+
# Follow `Topics` tags
5+
tags:
6+
- Array
7+
- Binary Search
8+
---
9+
10+
# [153. Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/)
11+
12+
## Description
13+
14+
You are given an array `nums` of length **n** that was originally sorted in ascending order but then rotated between 1 and **n** times. For example:
15+
16+
* `[0,1,2,4,5,6,7]` rotated 4 times becomes `[4,5,6,7,0,1,2]`.
17+
* `[0,1,2,4,5,6,7]` rotated 7 times remains `[0,1,2,4,5,6,7]`.
18+
19+
A single rotation moves the last element to the front:
20+
`[a[0], a[1], …, a[n-1]]``[a[n-1], a[0], a[1], …, a[n-2]]`.
21+
22+
Given such a rotated, sorted array of unique elements, return the **minimum element**.
23+
24+
The solution must run in **O(log n)** time complexity.
25+
26+
**Example 1:**
27+
```
28+
Input: nums = [10, 11 ,12 8, 9]
29+
Output: 8
30+
```
31+
32+
**Example 2:**
33+
```
34+
Input: nums = [4, 0, 1, 2, 3]
35+
Output: 0
36+
```
37+
38+
**Constraints:**
39+
40+
* `n == nums.length`
41+
* `1 <= n <= 5000`
42+
* `-5000 <= nums[i] <= 5000`
43+
* All the integers of `nums` are unique.
44+
* `nums` is sorted and rotated between `1` and `n` times.
45+
46+
## Solution
47+
48+
49+
```java
50+
class Solution {
51+
public int findMin(int[] nums) {
52+
int left = 0, right = nums.length - 1;
53+
while(left < right){
54+
int mid = left + (right - left) / 2;
55+
if(nums[mid] > nums[right]){
56+
left = mid + 1;
57+
}
58+
else{
59+
right = mid;
60+
}
61+
}
62+
return nums[left];
63+
}
64+
}
65+
```
66+
67+
```python
68+
class Solution:
69+
def findMin(self, nums: list[int]) -> int:
70+
left, right = 0, len(nums) - 1
71+
while left < right:
72+
mid = left + (right - left) // 2
73+
if nums[mid] > nums[right]:
74+
left = mid + 1
75+
else:
76+
right = mid
77+
return nums[left]
78+
```
79+
80+
## Complexity
81+
82+
- Time complexity: $$O(logn)$$
83+
<!-- Add time complexity here, e.g. $$O(n)$$ -->
84+
85+
- Space complexity: $$O(1)$$
86+
<!-- Add space complexity here, e.g. $$O(n)$$ -->
87+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int findMin(int[] nums) {
3+
int left = 0, right = nums.length - 1;
4+
while(left < right){
5+
int mid = left + (right - left) / 2;
6+
if(nums[mid] > nums[right]){
7+
left = mid + 1;
8+
}
9+
else{
10+
right = mid;
11+
}
12+
}
13+
return nums[left];
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def findMin(self, nums: list[int]) -> int:
3+
left, right = 0, len(nums) - 1
4+
while left < right:
5+
mid = left + (right - left) // 2
6+
if nums[mid] > nums[right]:
7+
left = mid + 1
8+
else:
9+
right = mid
10+
return nums[left]

0 commit comments

Comments
 (0)