-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob_456.java
171 lines (160 loc) · 6.09 KB
/
prob_456.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class prob_456 {
public static void main(String[] args) {
Solution_456 solution = new Solution_456();
int[] nums = {3,1,4,2};
int[] nums1 = {-2,1,2,-2,1,2};
System.out.println(solution.find132pattern_2(nums1));
}
}
class Solution_456 {
public boolean find132pattern(int[] nums) {
List<IntPair> ascendingPairs = new ArrayList<>();
int tmpMax = Integer.MAX_VALUE, tmpMin = Integer.MIN_VALUE;
for (int num : nums) {
if(num < tmpMax) {
ascendingPairs.add(new IntPair(tmpMin, tmpMax));
tmpMin = num;
}
tmpMax = num;
for (int i = 1; i < ascendingPairs.size(); i++) {
IntPair intPair = ascendingPairs.get(i);
if(num < intPair.getMax() && num > intPair.getMin()) {
return true;
}
}
}
return false;
}
public boolean find132pattern_1(int[] nums) {
List<IntPair> intervals = new ArrayList<>();
int tmpMin = nums[0], tmpMax = nums[0];
int i;
for (i = 1; i < nums.length; i++) {
if(nums[i] > nums[i-1]) {
intervals.add(new IntPair(nums[i-1], nums[i]));
tmpMin = nums[i-1];
tmpMax = nums[i];
i++;
break;
}
}
for (; i < nums.length; i++) {
if(nums[i] < nums[i-1]) {
for (int j = 0; j < intervals.size(); j++) {
if(tmpMin > intervals.get(j).getMax()) {
continue;
} else if(tmpMax < intervals.get(j).getMin()) {
intervals.add(j, new IntPair(tmpMin, tmpMax));
break;
} else {
int start = j, end = j;
boolean endInterval = true;
if(tmpMin < intervals.get(j).getMin()) {
while(j >= 0) {
if(!(tmpMin > intervals.get(j).getMax() || tmpMax < intervals.get(j).getMin())) {
tmpMin = Math.min(tmpMin, intervals.get(j).getMin());
tmpMax = Math.max(tmpMax, intervals.get(j).getMax());
start = j;
j--;
} else {
intervals.add(j+1, new IntPair(tmpMin, tmpMax));
for (int k = start+1; k <= end+1 ; k++) {
intervals.remove(k);
}
endInterval = false;
break;
}
}
if(endInterval) {
intervals.add(0, new IntPair(tmpMin, tmpMax));
for (int k = start+1; k <= end+1 ; k++) {
intervals.remove(k);
}
}
} else {
while(j < intervals.size()) {
if(!(tmpMin > intervals.get(j).getMax() || tmpMax < intervals.get(j).getMin())) {
tmpMin = Math.min(tmpMin, intervals.get(j).getMin());
tmpMax = Math.max(tmpMax, intervals.get(j).getMax());
end = j;
j++;
} else {
intervals.add(j, new IntPair(tmpMin, tmpMax));
for (int k = start; k <= end ; k++) {
intervals.remove(k);
}
endInterval = false;
break;
}
}
if(endInterval) {
intervals.add(new IntPair(tmpMin, tmpMax));
for (int k = start; k <= end ; k++) {
intervals.remove(k);
}
}
}
break;
}
}
tmpMin = nums[i];
}
tmpMax = nums[i];
for (IntPair interval : intervals) {
if(nums[i] < interval.getMax() && nums[i] > interval.getMin()) {
return true;
}
}
}
return false;
}
/**
* The idea is to compare the j and k numbers first since they are adjacent to each other and then finding iterating down for possible i's
* Finding the max value of the all possible k's smaller than j (done through stack)
* @param nums
* @return
*/
public boolean find132pattern_2(int[] nums) {
Stack<Integer> stack = new Stack<>();
int tmpMax = Integer.MIN_VALUE;
for (int i = nums.length - 1; i >= 0; i--) {
if(nums[i] < tmpMax) {
return true;
} else {
while(!stack.empty() && nums[i] > stack.peek()) {
tmpMax = stack.peek();
stack.pop();
}
}
stack.push(nums[i]);
}
return false;
}
}
class IntPair {
private int min;
private int max;
public IntPair(int min, int max) {
this.min = min;
this.max = max;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
@Override
public String toString() {
return (this.getMin()+"-"+this.getMax());
}
}