What is the problem with my code?


  • 0
    J

    Cannot pass test case [33,53,12,64,50,41,45,21,97,35,47,92,39,0,93,55,40,46,69,42,6,95,51,68,72,9,32,84,34,64,6,2,26,98,3,43,30,60,3,68,82,9,97,19,27,98,99,4,30,96,37,9,78,43,64,4,65,30,84,90,87,64,18,50,60,1,40,32,48,50,76,100,57,29,63,53,46,57,93,98,42,80,82,9,41,55,69,84,82,79,30,79,18,97,67,23,52,38,74,15]
    Thanks for pointing out the problem...

    public class Solution {
        public int wiggleMaxLength(int[] nums) {
            if(nums==null||nums.length==0) return 0;
            if(nums.length<2) return nums.length;
            int[] p=new int[nums.length];//n>n-1
            int[] q=new int[nums.length];//n<n-1
            Arrays.fill(p,1);
            Arrays.fill(q,1);
            int res=0;
            for(int i=1;i<nums.length;i++){
                int max1=Integer.MIN_VALUE;//for p
                int max2=Integer.MIN_VALUE;//for q
                for(int j=0;j<i;j++){
                    if(nums[j]<nums[i]&&q[j]+1>max1){
                        max1=Math.max(p[i],q[j]+1);
                    }
                    if(nums[j]>nums[i]&&p[j]>max2){
                        max2=Math.max(q[i],p[j]+1);
                    }
                }
                p[i]=(max1==Integer.MIN_VALUE?1:max1);
                q[i]=(max2==Integer.MIN_VALUE?1:max2);
            }
            return p[nums.length-1]>q[nums.length-1]?p[nums.length-1]:q[nums.length-1];
        }
    }
    

Log in to reply
 

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