Shortest logn answer


  • 2
    Q

    We can just compare the beginning and end of the array, and update the low and high indices accordingly.

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

Log in to reply
 

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