simple C++(Your runtime beats 100.00 % of cpp submissions ~)


  • 0
    Q
    int findUnsortedSubarray(vector<int>& nums) {
            int l , r ;
            int mi = INT_MIN;
            l = r = -1 ;
            for(int i = 0 ; i < nums.size() ; ++i){
                if( mi > nums[i] )r = i;
                mi = max(mi,nums[i]);
                if( i && nums[i] < nums[i-1]  ){
                    if( l == -1 )l = i - 1;
                    while( nums[i] < nums[l-1] && l )l--;
                }
            }
            if( l == - 1 )return 0;
            return  r - l + 1;
        }
    

Log in to reply
 

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