153. Find Minimum in Rotated Sorted Array

Description:
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/#/description

Algorithm:
scan, if nums[i+1] < nums[i], nums[i+1] will be the minimum element

Code:
class Solution {
public:
int findMin(vector& nums) {
int minEle = INT_MIN;
for (int i = 0; i < nums.size() - 1;i++)
{
if (nums[i+1] < nums[i])
minEle = nums[i+1];
}
if (minEle == INT_MIN)
minEle = nums[0];
return minEle;
}
};

Timing & Space:
O(n) & O(1)

Leave a Reply

Your email address will not be published. Required fields are marked *