Increasing Triplet Subsequence Problem


Description

LeetCode Problem 334.

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:

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

Example 2:

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

Example 3:

1
2
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 * 10^5
  • -2^31 <= nums[i] <= 2^31 - 1


Sample C++ Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
    bool increasingTriplet(vector<int>& nums) {
        int len = nums.size();
        
        if (len < 3)
            return false;
        
        int a = INT_MAX, b = INT_MAX;
        
        for (int i = 0; i < len; i ++) {
            if (nums[i] <= a)
                a = nums[i];
            else if (nums[i] <= b)
                b = nums[i];
            else 
                return true;
        }
        return false;
    }
};




Related Posts

Lemonade Change Problem

LeetCode 860. At a lemonade stand, each lemonade costs $5....

Set Intersection Size At Least Two Problem

LeetCode 757. You are given a 2D integer array intervals...

Maximum Swap Problem

LeetCode 670. You are given an integer num. You can...

Can Place Flowers Problem

LeetCode 605. You have a long flowerbed in which some...

Super Washing Machines Problem

LeetCode 517. You have n super washing machines on a...

Minimum Number Of Arrows To Burst Balloons Problem

LeetCode 452. There are some spherical balloons taped onto a...