Find Minimum in Rotated Sorted Array – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
Suppose an array of length n
sorted in ascending order is rotated between 1
and n
times. For example, the array nums = [0,1,2,4,5,6,7]
might become:
[4,5,6,7,0,1,2]
if it was rotated4
times.[0,1,2,4,5,6,7]
if it was rotated7
times.
Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]]
1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]
.
Given the sorted rotated array nums
of unique elements, return the minimum element of this array.
You must write an algorithm that runs in O(log n) time.
Example 1:
Input: nums = [3,4,5,1,2] Output: 1 Explanation: The original array was [1,2,3,4,5] rotated 3 times.
Example 2:
Input: nums = [4,5,6,7,0,1,2] Output: 0 Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
Example 3:
Input: nums = [11,13,15,17] Output: 11 Explanation: The original array was [11,13,15,17] and it was rotated 4 times.
Constraints:
n == nums.length
1 <= n <= 5000
-5000 <= nums[i] <= 5000
- All the integers of
nums
are unique. nums
is sorted and rotated between1
andn
times.
C++ Find Minimum in Rotated Sorted Array LeetCode Solution
int findMin(vector<int> &num) {
int start=0,end=num.size()-1;
while (start<end) {
if (num[start]<num[end])
return num[start];
int mid = (start+end)/2;
if (num[mid]>=num[start]) {
start = mid+1;
} else {
end = mid;
}
}
return num[start];
}
Java Find Minimum in Rotated Sorted Array LeetCode Solution
public class Solution {
public int findMin(int[] num) {
if (num == null || num.length == 0) {
return 0;
}
if (num.length == 1) {
return num[0];
}
int start = 0, end = num.length - 1;
while (start < end) {
int mid = (start + end) / 2;
if (mid > 0 && num[mid] < num[mid - 1]) {
return num[mid];
}
if (num[start] <= num[mid] && num[mid] > num[end]) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return num[start];
}
}
Python 3 Find Minimum in Rotated Sorted Array LeetCode Solution
class Solution:
def findMin(self, nums: List[int]) -> int:
if len(nums) == 1 or nums[0] < nums[-1]:
return nums[0]
left, right = 0, len(nums) - 1
while left <= right:
mid = left + (right - left) // 2
if mid > 0 and nums[mid - 1] > nums[mid]: # The nums[mid] is the minimum number
return nums[mid]
if nums[mid] > nums[right]: # search on the right side, because smaller elements are in the right side
left = mid + 1
else:
right = mid - 1 # search the minimum in the left side
Array-1180
String-562
Hash Table-412
Dynamic Programming-390
Math-368
Sorting-264
Greedy-257
Depth-First Search-256
Database-215
Breadth-First Search-200
Tree-195
Binary Search-191
Matrix-176
Binary Tree-160
Two Pointers-151
Bit Manipulation-140
Stack-133
Heap (Priority Queue)-117
Design-116
Graph-108
Simulation-103
Prefix Sum-96
Backtracking-92
Counting-86
Sliding Window-73
Linked List-69
Union Find-66
Ordered Set-48
Monotonic Stack-47
Recursion-43
Trie-41
Binary Search Tree-40
Divide and Conquer-40
Enumeration-39
Bitmask-37
Queue-33
Memoization-32
Topological Sort-31
Geometry-30
Segment Tree-27
Game Theory-24
Hash Function-24
Binary Indexed Tree-21
Interactive-18
Data Stream-17
String Matching-17
Rolling Hash-17
Shortest Path-16
Number Theory-16
Combinatorics-15
Randomized-12
Monotonic Queue-9
Iterator-9
Merge Sort-9
Concurrency-9
Doubly-Linked List-8
Brainteaser-8
Probability and Statistics-7
Quickselect-7
Bucket Sort-6
Suffix Array-6
Minimum Spanning Tree-5
Counting Sort-5
Shell-4
Line Sweep-4
Reservoir Sampling-4
Eulerian Circuit-3
Radix Sort-3
Strongly Connected Componen-t2
Rejection Sampling-2
Biconnected Component-1
Leave a comment below