Given an integer array nums, return true if there exists a triple of indices $\[i, j, k\]$ such that $ i j k$ and numsi < numsj < numsk. If no such indices exists, return false.
Example:
1 | Input: nums = [1, 2, 3, 4, 5]; |
1 | Input: nums = [5, 4, 3, 2, 1]; |
1 | Input: nums = [2, 1, 5, 0, 4, 6]; |
Constraints: - $ 1 l_{nums} ^5 $ - $ -2^{31} nums_i ^{31}-1$
Solution
1 | class Solution { |
Explanation: The solution is based on keeping track of two minimum values, i and j, while iterating through the array.
- Initialize two variables, i and j, to INT_MAX.
- If the element is less than or equal to i, update i with the element.
- If the element if greater than i but less than or equal to j, update j with the element.
- If the element is greater than both i and j, it means an increasing triplet is found, and the function returns true.
- If the loop completes without finding an increasing triplet, return false.
The time complexity is O(n) and the space complexity is O(1).