Skip to content

Commit bde3371

Browse files
committed
solve 334.increasing-triplet-subsequence
1 parent 2da76f1 commit bde3371

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* @lc app=leetcode id=334 lang=java
3+
*
4+
* [334] Increasing Triplet Subsequence
5+
*
6+
* https://leetcode.com/problems/increasing-triplet-subsequence/description/
7+
*
8+
* algorithms
9+
* Medium (39.55%)
10+
* Likes: 971
11+
* Dislikes: 96
12+
* Total Accepted: 103.8K
13+
* Total Submissions: 262.4K
14+
* Testcase Example: '[1,2,3,4,5]'
15+
*
16+
* Given an unsorted array return whether an increasing subsequence of length 3
17+
* exists or not in the array.
18+
*
19+
* Formally the function should:
20+
*
21+
* Return true if there exists i, j, k
22+
* such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return
23+
* false.
24+
*
25+
* Note: Your algorithm should run in O(n) time complexity and O(1) space
26+
* complexity.
27+
*
28+
*
29+
* Example 1:
30+
*
31+
*
32+
* Input: [1,2,3,4,5]
33+
* Output: true
34+
*
35+
*
36+
*
37+
* Example 2:
38+
*
39+
*
40+
* Input: [5,4,3,2,1]
41+
* Output: false
42+
*
43+
*
44+
*
45+
*/
46+
class Solution {
47+
public boolean increasingTriplet(int[] nums) {
48+
int small = Integer.MAX_VALUE;
49+
int big = Integer.MAX_VALUE;
50+
for (int num : nums) {
51+
if (num <= small) {
52+
small = num;
53+
} else if (num <= big) {
54+
big = num;
55+
} else {
56+
return true;
57+
}
58+
}
59+
return false;
60+
}
61+
}
62+

0 commit comments

Comments
 (0)