-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path334.java
49 lines (44 loc) · 1.5 KB
/
334.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public boolean increasingTriplet(int[] a) {
if (a.length < 3) {
return false;
}
int small = Integer.MAX_VALUE; // smallest ending integer of subsequence with length 1
int large = Integer.MAX_VALUE; // smallest ending integer of ascending subsequence with length 2
for (int num : a) {
if (num < small) {
small = num;
} else if (num > small && num < large) {
large = num;
} else if (num > large) {
// when num > large, we are sure that there exists a number that is before "large" and smaller than "large"
return true;
}
}
return false;
}
}
__________________________________________________________________________________________________
sample 36492 kb submission
public class Solution {
public boolean increasingTriplet(int[] nums) {
if (nums == null || nums.length < 3) {
return false;
}
int[] cache = new int[2];
cache[0] = cache[1] = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > cache[1]) {
return true;
} else if (nums[i] > cache[0] && nums[i] <= cache[1]) {
cache[1] = nums[i];
} else {
cache[0] = nums[i];
}
}
return false;
}
}
__________________________________________________________________________________________________