-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode334.java
More file actions
66 lines (61 loc) · 1.93 KB
/
LeetCode334.java
File metadata and controls
66 lines (61 loc) · 1.93 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public class LeetCode334 {
public static void main(String[] args) {
// 输入:nums = [1,2,3,4,5]
// 输出:true
System.out.println(new Solution334_2().increasingTriplet(new int[] { 1, 2, 3, 4, 5 }));
// 输入:nums = [5,4,3,2,1]
// 输出:false
System.out.println(new Solution334_2().increasingTriplet(new int[] { 5, 4, 3, 2, 1 }));
// 输入:nums = [2,1,5,0,4,6]
// 输出:true
System.out.println(new Solution334_2().increasingTriplet(new int[] { 2, 1, 5, 0, 4, 6 }));
}
}
/**
* 贪心算法(非常tricky)
*/
class Solution334_1 {
public boolean increasingTriplet(int[] nums) {
int n = nums.length;
if (n < 3) {
return false;
}
int first = nums[0], second = Integer.MAX_VALUE;
for (int i = 1; i < n; i++) {
int num = nums[i];
if (num > second) {
return true;
} else if (num > first) {
second = num;
} else {
first = num;
}
}
return false;
}
}
/**
* 双向遍历找最值
*/
class Solution334_2 {
public boolean increasingTriplet(int[] nums) {
int[] leftMin = new int[nums.length];
int[] rightMax = new int[nums.length];
leftMin[0] = nums[0];
for (int i = 1; i < nums.length; i++) {
leftMin[i] = Math.min(leftMin[i - 1], nums[i]);
}
rightMax[nums.length - 1] = nums[nums.length - 1];
for (int i = nums.length - 2; i >= 0; i--) {
rightMax[i] = Math.max(rightMax[i + 1], nums[i]);
}
// System.out.println(Arrays.toString(leftMin));
// System.out.println(Arrays.toString(rightMax));
for (int i = 1; i <= nums.length - 2; i++) {
if (leftMin[i] < nums[i] && nums[i] < rightMax[i]) {
return true;
}
}
return false;
}
}