LeetCode - Algorithms - 334. Increasing Triplet Subsequence

Problem

334. Increasing Triplet Subsequence

Follow up: Could you implement a solution that runs in O(n) time complexity and O(1) space complexity?

Java

Increasing triplet subsequence problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public boolean increasingTriplet(int[] nums) {
int first = Integer.MAX_VALUE;
int second = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
if (nums[i] < first)
first = nums[i];
if (nums[i] > first && nums[i] < second) {
second = nums[i];
}
if (nums[i] > second)
return true;
}
return false;
}
}

Submission Detail

  • 75 / 75 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Increasing Triplet Subsequence.
  • Memory Usage: 39.1 MB, less than 9.26% of Java online submissions for Increasing Triplet Subsequence.