Java clean one pass solution O(1) space


  • 1
    J
     public boolean increasingTriplet(int[] nums) {
        int len = nums.length;
        if (len < 3) {
          return false;
        }
        int small = nums[0];
        int middle = Integer.MAX_VALUE;
        for (int cur = 1; cur < len; cur++) {
          if (nums[cur] > middle) {
            return true;
          }
          if (nums[cur] < middle && nums[cur] > small) {
            middle = nums[cur];
          } else if (nums[cur] < small) {
            small = nums[cur];
          }
        }
        return false;
      }
    

    The key is to maintain two variables small and middle which satisfy small < middle all the time.


Log in to reply
 

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