A simlpe binary search solution.


  • 0
    F

    The idea is to find the first element that smaller than num[0]. Take care if num is not rotating.

    class Solution {
    public:
    	int findMin(vector<int> &num) {
    		if (num[0] < num[num.size() - 1])
    			return num[0];
    		
    		int L = -1, R = num.size() - 1;
    
    		while (R - L > 1)
    		{
    			int M = (L + R) >> 1;
    			if (num[M] >= num[0])
    				L = M;
    			else
    				R = M;
    		}
    
    		return num[R];
    	}
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.