Concise 10 lines code, 0ms acepted!


  • 1
    M
    public int wiggleMaxLength(int[] a) {
           if(a.length<2) return a.length;
           int prev = a[0], maxLen=1
           boolean increasing = a[1]>a[0];   // denoting if we are expecting increased relative to prev
     
           for(int i=1; i<a.length; i++){
               if ( (increasing && (a[i] >prev)) || (!increasing && (a[i] < prev) ) ) {
                       increasing = !increasing;
                       maxLen++;
               }
               prev = a[i];
           }
           return maxLen;
    }
    

Log in to reply
 

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