Accept C++,simple!


  • -1
    K

    int findMin(vector<int> &num) {
    int n=num.size();
    int min1,min2;
    for(int i=1;i<n-1;i++)
    {
    if(num[i]<num[i-1]&&num[i]<num[i+1])
    return num[i];
    }
    return num[0]>num[n-1]?num[n-1]:num[0];
    }


  • 0
    W

    Well, if the vector's min element is the last one, looks like {4,5,6,7,0}, your code will have a O(n) complexity. I think you should think about using binary search.


  • 0
    K

    Thanks for your advice!


Log in to reply
 

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