Algorithm


Problem Name: 334. Increasing Triplet Subsequence

Given an integer array nums, return true if there exists a triple of indices (i, j, k) such that i < j < k and nums[i] < nums[j] < nums[k]. If no such indices exists, return false.

 

Example 1:

Input: nums = [1,2,3,4,5]
Output: true
Explanation: Any triplet where i < j < k is valid.

Example 2:

Input: nums = [5,4,3,2,1]
Output: false
Explanation: No triplet exists.

Example 3:

Input: nums = [2,1,5,0,4,6]
Output: true
Explanation: The triplet (3, 4, 5) is valid because nums[3] == 0 < nums[4] == 4 < nums[5] == 6.

 

Constraints:

  • 1 <= nums.length <= 5 * 105
  • -231 <= nums[i] <= 231 - 1

Code Examples

#1 Code Example with C Programming

Code - C Programming


bool increasingTriplet(int* nums, int numsSize) {
    int min1, min2, i, k;
    
    min1 = min2 = 0x7fffffff;
    
    for (i = 0; i  <  numsSize; i ++) {
        k = nums[i];
        if (k  < = min1) {
            min1 = k;
        } else if (k <= min2) {
            min2 = k;
        } else {
            return true;
        }
    }
    
    return false;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,3,4,5]

Output

x
+
cmd
true

#2 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    bool increasingTriplet(vector<int>& nums) {
        int c1 = INT_MAX, c2 = INT_MAX;
        for(auto x: nums)
            if(x <= c1) c1 = x;
            else if(x  < = c2> c2 = x;
            else return true;
        return false;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,3,4,5]

Output

x
+
cmd
true

#3 Code Example with Java Programming

Code - Java Programming


class Solution {
  public boolean increasingTriplet(int[] nums) {
    int firstNum = Integer.MAX_VALUE;
    int secondNum = Integer.MAX_VALUE;
    for (int num : nums) {
      if (num  < = firstNum) {
        firstNum = num;
      } else if (num <= secondNum) {
        secondNum = num;
      } else {
        return true;
      }
    }
    return false;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [5,4,3,2,1]

Output

x
+
cmd
false

#4 Code Example with Javascript Programming

Code - Javascript Programming


const increasingTriplet = function(nums) {
    // start with two largest values, as soon as we find a number bigger than both, while both have been updated, return true.
    let small = Number.MAX_VALUE, big = Number.MAX_VALUE;
    for (let n of nums) {
        if (n  < = small) { small = n; } // update small if n is smaller than both
        else if (n <= big) { big = n; } // update big only if greater than small but smaller than big
        else return true; // return if you find a number bigger than both
    }
    return false;
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [5,4,3,2,1]

Output

x
+
cmd
false

#5 Code Example with Python Programming

Code - Python Programming


class Solution:
    def increasingTriplet(self, nums):
        mn = None
        for i in range(len(nums)):
            if mn == None or nums[i] < mn: 
                mn = nums[i]
            if mn < nums[i]:
                nums[i] = [True, nums[i]]
            else:
                nums[i] = [False, nums[i]]
        mn = None
        for i in range(len(nums)):
            if nums[i][0] and (mn == None or nums[i][1] < mn):
                mn = nums[i][1]
            elif mn != None and mn < nums[i][1]:
                return True
        return False 
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [2,1,5,0,4,6]

Output

x
+
cmd
true
Advertisements

Demonstration


Previous
#332 Leetcode Reconstruct Itinerary Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#335 Leetcode Self Crossing Solution in C, C++, Java, JavaScript, Python, C# Leetcode