Easy Java Solution with maintaining previousMax and currentMax


  • 0
    M
    public boolean checkPossibility(int[] nums) {
            int prev = Integer.MIN_VALUE;
            int cur = Integer.MIN_VALUE;
            boolean first = false;
            for(int i =0;i< nums.length; i++)
            {
                if(nums[i] >= cur)
                {
                    prev = cur;
                    cur = nums[i];
                }
                else
                {
                    if(first == true) return false;
                    first = true;
                    if(nums[i] <= prev)
                    {
                        prev = cur;
                    }
                    else
                    {
                        cur = nums[i];
                        prev = nums[i]; 
                    }
                }
            }
    	    return true;
        
        }
    

Log in to reply
 

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