Share my java ac


  • 0
    M
    public int wiggleMaxLength(int[] nums) {
        if(nums.length<1) return 0;
                
        return Math.max(helper(nums, true), helper(nums, false));
    }
    private int helper(int[] nums, boolean isUp){
        int cnt = 1;
        for(int i=0;i<nums.length-1;i++){
            if(!isUp && nums[i]<nums[i+1]){
                cnt++;
                isUp = !isUp;
            }if(isUp && nums[i]>nums[i+1]){
                cnt++;
                isUp = !isUp;
            }        
        }
        return cnt;        
    }

Log in to reply
 

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