-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncreasingTripletSubsequence.java
More file actions
278 lines (228 loc) · 8.27 KB
/
IncreasingTripletSubsequence.java
File metadata and controls
278 lines (228 loc) · 8.27 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package Algorithms.IntegerArray;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 06 April 2025
*/
public class IncreasingTripletSubsequence {
public static void main(String[] args) {
int[] nums = {1,5,2,0,3,6};
System.out.println("increasingTripletBruteForce -> " + increasingTripletBruteForce(nums)); // Output: true
System.out.println("increasingTriplet -> " + increasingTriplet(nums)); // Output: true
System.out.println("increasingTriplet2 -> " + increasingTriplet2(nums)); // Output: true
System.out.println("increasingTriplet3 -> " + increasingTriplet3(nums)); // Output: true
System.out.println("increasingTripletUsingDFS -> " + increasingTripletUsingDFS(nums)); // Output: true
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(1)
*
* PATTERNS:
* ---------
* 1) [1,5,2,0,3,6] --> here [1,5,6], [1,2,3] and [0,3,6] are true as i<j<k and nums[i]<nums[j]<nums[k]
* 2) [20,100,10,12,5,13] --> here [10,12,13] is true
* 3) Move until you find smallest number
* 4) [5,6,1,2,7]
*
* [5,6,1,2,7]
* i j
* [5,6,1,2,7]
* j i
* [5,6,1,2,7]
* i j
* [5,6,1,2,7]
* i j |
* |
* else case --> i.e true
*
* 5) [1,2,7,3,4]
*
* [1,2,7,3,4]
* i j
* [1,2,7,3,4]
* |
* else case --> i.e true
*
*/
public static boolean increasingTriplet(int[] nums) {
int first = Integer.MAX_VALUE, second = Integer.MAX_VALUE;
for (int num : nums) {
if (num <= first) {
first = num;
} else if (num <= second) {
second = num;
} else { // num > first && num > second -- num greater than both first and second
return true;
}
}
return false;
}
/** same as above {@link #increasingTriplet(int[])} */
public static boolean increasingTriplet2(int[] nums) {
int first = Integer.MAX_VALUE, second = Integer.MAX_VALUE;
for (int num : nums) {
if (num <= first)
first = num;
if (num>first && num <= second)
second = num;
if (num>first && num>second) // or just if(num > second)
return true; // Found a number greater than both first and second
}
return false;
}
/** same as above {@link #increasingTriplet(int[])} */
public static boolean increasingTriplet3(int[] nums) {
int first = Integer.MAX_VALUE, second = Integer.MAX_VALUE;
for (int num : nums) {
if (num <= first)
first = num;
else if (num>first && num <= second)
second = num;
else if (num>first && num>second) // or just if(num > second)
return true; // Found a number greater than both first and second
}
return false;
}
public static boolean increasingTriplet4(int[] nums) {
int first = Integer.MAX_VALUE, second = Integer.MAX_VALUE;
for (int num : nums) {
if(num > second) return true; // found a number greater than both first and second
if(num <= first) first = num; // replace first with nums[i]
else if(num <= second) second = num; // replace second with nums[i]
}
return false;
}
public static boolean increasingTriplet5(int[] nums) {
int first = Integer.MAX_VALUE, second = Integer.MAX_VALUE;
for (int num : nums) {
if (num > second) return true; // found a number greater than both first and second
if (num > first) second = Math.min(second, num); // replace second with nums[i] if it's smaller than current second
first = Math.min(first, num);
}
return false;
}
/**
* @TimeComplexity: O(n^2)
* @SpaceComplexity: O(1)
*/
public static boolean increasingTripletUsingDFS(int[] nums) {
int n= nums.length;
if(n < 3) return false;
int first = Integer.MAX_VALUE, second = Integer.MAX_VALUE;
return dfs(0, nums, first, second);
}
public static boolean dfs(int i, int[] nums, int first, int second) {
if (i == nums.length) return false;
if (nums[i] <= first) return dfs(i+1, nums, nums[i], second); // replace first with nums[i]
else if (nums[i] <= second) return dfs(i+1, nums, first, nums[i]); // replace second with nums[i]
else return true; // found a number greater than both first and second
}
/**
* same as {@link Algorithms.DynamicProgramming.LongestIncreasingSubsequence#lengthOfLIS(int[])}
*
* TLE
* @TimeComplexity O(n^2)
* @SpaceComplexity O(n)
*/
public boolean increasingTripletUsingLIS(int[] nums) { // Longest Increasing Subsequence
int n = nums.length;
int[] LIS = new int[n]; // Longest Increasing Subsequence for each index
Arrays.fill(LIS, 1);
for (int i = n-1; i >= 0; i--) {
for (int j = i+1; j < n; j++) {
if (nums[i] < nums[j])
LIS[i] = Math.max(LIS[i], 1+LIS[j]);
// if (LIS[i] >= 3) return true;
}
}
return Arrays.stream(LIS).filter(c->c==3).count() > 0;
}
/**
* TLE
* @TimeComplexity O(n^3)
* @SpaceComplexity O(1)
*
* We can use brute force or DFS or dynamic programming to solve this problem.
*/
public static boolean increasingTripletBruteForce(int[] nums) {
int n = nums.length;
if(n < 3) return false;
for(int i=0; i<n; i++) {
int firstNum = nums[i];
// find secondNum
for (int j=i+1; j<n; j++) {
int secondNum = nums[j];
// find thirdNum
if(firstNum < secondNum) {
for (int k=j+1; k<n; k++) {
int thirdNum = nums[k];
if(secondNum < thirdNum)
return true;
}
}
}
}
return false;
}
// TLE
public static boolean increasingTripletDP(int[] nums) {
int n = nums.length;
if(n < 3) return false;
// dp[i] = length of longest increasing subsequence ending at index i
int[] dp = new int[n];
for (int i=0; i<n; i++) {
dp[i] = 1; // every element is a subsequence of length 1
for (int j=0; j<i; j++) {
if(nums[j] < nums[i]) {
dp[i] = Math.max(dp[i], dp[j]+1);
}
}
if(dp[i] >= 3) return true;
}
return false;
}
// NOT WORKING
public boolean increasingTripletUsingHashMap(int[] nums) {
int n = nums.length;
Map<Integer, Map<Integer, Integer>> map = new HashMap<>();
for (int i=0; i<n; i++) map.computeIfAbsent(i, _->new LinkedHashMap<>()).put(nums[i], 1);
if(n < 3) return false;
for(int i=0; i<n; i++) {
for(int j=i+1; j<n; j++) {
if (nums[i] <= nums[j]) {
map.get(i).merge(nums[j], 1, Integer::sum);
}
}
}
for(int k: map.keySet()) {
Map<Integer, Integer> counterMap = map.get(k);
System.out.println(counterMap);
if (isIncreasingTriplets( counterMap )) return true;
}
return false;
}
public boolean isIncreasingTriplets(Map<Integer, Integer> counterMap) {
int n = counterMap.size();
if(n < 3) return false;
List<Integer> lst = new ArrayList<>(counterMap.keySet());
int c = 0;
int firstNum = lst.get(0);
c+=counterMap.get(firstNum);
if(c==3) return true;
return false;
}
// ONLY WORKS FOR CONSECUTIVE INDICES
public boolean increasingTripletIfConsecutiveIndices(int[] nums) {
int n = nums.length;
if(n < 3) return false;
for(int i=0; i<n-2; i++) {
if(nums[i] < nums[i+1] && nums[i+1] < nums[i+2]) return true;
}
return false;
}
}