HomeLeetcode334. Increasing Triplet Subsequence - Leetcode Solutions

334. Increasing Triplet Subsequence – Leetcode Solutions

Description

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.

Examples:

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.

Solution in Python

To solve the problem of finding if there exists an increasing triplet subsequence in the array nums, we can leverage a greedy approach. The goal is to find three indices i < j < k such that nums[i] < nums[j] < nums[k].

Key Idea:

We don’t need to track the exact indices of the triplet; we just need to determine if such a subsequence exists. The idea is to maintain two variables first and second which will represent the smallest and second smallest elements encountered so far, respectively. As we traverse through the array:

  • We update first to the smallest value encountered.
  • We update second when we find a number larger than first but smaller than second.
  • If we find a number larger than both first and second, we know a triplet exists, and we can return True.

Algorithm:

  1. Initialize two variables first and second with large values to represent the smallest and the second smallest numbers encountered so far.
  2. Traverse the array nums.
    • If the current element is smaller than or equal to first, update first to this value (since we are looking for the smallest possible number).
    • If the current element is larger than first but smaller than or equal to second, update second.
    • If the current element is larger than both first and second, return True since we have found an increasing triplet.
  3. If we complete the loop without finding such a triplet, return False.
Python
class Solution:
    def increasingTriplet(self, nums: List[int]) -> bool:
        # Initialize two variables to track the smallest and second smallest numbers
        first = float('inf')
        second = float('inf')
        
        # Traverse through the array
        for num in nums:
            if num <= first:
                # If current number is smaller than or equal to 'first', update 'first'
                first = num
            elif num <= second:
                # If current number is greater than 'first' but smaller than or equal to 'second', update 'second'
                second = num
            else:
                # If we find a number larger than both 'first' and 'second', return True
                return True
        
        # If no increasing triplet is found, return False
        return False

Explanation of the Code:

  1. first and second initialization: We start with first and second set to infinity (float('inf')), which ensures that any number in the array will be smaller initially.
  2. Loop through nums: For each number num in the array:
    • If num is less than or equal to first, it means we have found a new potential smallest number, so we update first.
    • If num is larger than first but less than or equal to second, we update second, as this could be a potential second smallest number.
    • If num is greater than both first and second, we return True because we have found a triplet that satisfies the condition.
  3. Final return: If the loop finishes without returning True, that means no increasing triplet was found, so we return False.

Solution in C++

C++
class Solution {
public:
    bool increasingTriplet(std::vector<int>& nums) {
        // Edge case: If the array has less than 3 elements, we cannot have a triplet
        if (nums.size() < 3) {
            return false;
        }
        
        // Initialize two variables with large values
        int first = INT_MAX, second = INT_MAX;
        
        // Traverse through the array
        for (int num : nums) {
            // If the current number is smaller than or equal to the first, update the first
            if (num <= first) {
                first = num;
            }
            // If the current number is greater than first but smaller than or equal to the second, update the second
            else if (num <= second) {
                second = num;
            }
            // If the current number is greater than both first and second, we found a triplet
            else {
                return true;
            }
        }
        
        // No increasing triplet was found
        return false;
    }
};

Solution in Javascript

JavaScript
var increasingTriplet = function(nums) {
    // Initialize two variables, first and second, to hold the smallest and second smallest values we've encountered.
    let first = Infinity;
    let second = Infinity;
    
    // Loop through the array of numbers
    for (let num of nums) {
        // Check if the current number is smaller than or equal to the 'first' variable
        // If so, update 'first' to the current number. This means we're still looking for the smallest number.
        if (num <= first) {
            first = num;
        } 
        // Otherwise, check if the current number is smaller than or equal to 'second'
        // and greater than 'first'. If so, update 'second' to the current number. This means
        // we're looking for the second number in the increasing triplet.
        else if (num <= second) {
            second = num;
        } 
        // If the current number is greater than both 'first' and 'second', then we have found the third number
        // in the increasing triplet and can return true.
        else {
            return true;
        }
    }

    // If we complete the loop without finding a valid increasing triplet, return false.
    return false;
};

Solution in Java

Java
class Solution {
    public boolean increasingTriplet(int[] nums) {
        // Step 1: Check for array length. If it's less than 3, it's impossible to find a triplet
        if (nums.length < 3) {
            return false;
        }

        // Step 2: Initialize two variables to track the first and second smallest elements
        int first = Integer.MAX_VALUE;  // This will track the smallest number
        int second = Integer.MAX_VALUE; // This will track the second smallest number

        // Step 3: Traverse through the array and attempt to find an increasing triplet
        for (int num : nums) {
            // If the current number is smaller than or equal to 'first', update 'first'
            if (num <= first) {
                first = num;
            } 
            // If the current number is smaller than or equal to 'second' but larger than 'first', update 'second'
            else if (num <= second) {
                second = num;
            } 
            // If we find a number greater than both 'first' and 'second', we have found a valid triplet
            else {
                return true;
            }
        }

        // Step 4: If we complete the loop without finding such a triplet, return false
        return false;
    }
}

Subscribe
Notify of

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Popular