Java O(n) O(1) Solution


  • 0
    L
    public class Solution {
        public boolean increasingTriplet(int[] nums) {
            int[] triplet = new int[3];
            int i = 0, last = Integer.MAX_VALUE;
            for(int j=0; j<nums.length; j++){
                int num = nums[j];
                if(num > last) return true;
                if(i == 0){
                    triplet[i++] = num;
                }else if(i == 1){
                    if(num > triplet[0]){
                        triplet[i++] = num;
                    }else{
                        triplet[0] = num;
                    }
                }else{
                    if(num > triplet[1]){
                        return true;
                    }
                    if(num > triplet[0]){
                        triplet[1] = num;
                    }else{
                        triplet[0] = num;
                        i = 1;
                        last = triplet[1];
                    }
                }
            }
            return false;
        }
    }
    

Log in to reply
 

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