-
Notifications
You must be signed in to change notification settings - Fork 44
/
IncreasingTripletSubsequence.java
123 lines (110 loc) · 3.3 KB
/
IncreasingTripletSubsequence.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
package LeetCodeJava.Array;
// https://leetcode.com/problems/increasing-triplet-subsequence/description/
/**
* 334. Increasing Triplet Subsequence
* Solved
* Medium
* Topics
* Companies
* Given an integer array nums, return true if there exists a triple of indices (i, j, k) such that i < j < k and nums[i] < nums[j] < nums[k]. If no such indices exists, return false.
* <p>
* <p>
* <p>
* Example 1:
* <p>
* Input: nums = [1,2,3,4,5]
* Output: true
* Explanation: Any triplet where i < j < k is valid.
* Example 2:
* <p>
* Input: nums = [5,4,3,2,1]
* Output: false
* Explanation: No triplet exists.
* Example 3:
* <p>
* Input: nums = [2,1,5,0,4,6]
* Output: true
* Explanation: The triplet (3, 4, 5) is valid because nums[3] == 0 < nums[4] == 4 < nums[5] == 6.
* <p>
* <p>
* Constraints:
* <p>
* 1 <= nums.length <= 5 * 105
* -231 <= nums[i] <= 231 - 1
* <p>
* <p>
* Follow up: Could you implement a solution that runs in O(n) time complexity and O(1) space complexity?
*/
public class IncreasingTripletSubsequence {
// V0
public boolean increasingTriplet(int[] nums) {
// NOTE !!! we init as max integer
Integer biggest_1 = Integer.MAX_VALUE;
Integer biggest_2 = Integer.MAX_VALUE;
for (int x : nums){
// NOTE !!! condition below
if (biggest_1 >= x){
biggest_1 = x;
// NOTE !!! condition below
}else if (biggest_2 >= x){
biggest_2 = x;
}else{
return true;
}
}
return false;
}
// V1
// IDEA : LOOP (fixed by gpt)
public boolean increasingTriplet_1(int[] nums) {
if (nums.length < 3) {
return false;
}
int first = Integer.MAX_VALUE;
int second = Integer.MAX_VALUE;
for (int num : nums) {
if (num <= first) {
// update first if num is smaller than or equal to first
first = num;
} else if (num <= second) {
// update second if num is smaller than or equal to second
second = num;
} else {
// if we find a number greater than both first and second, return true
return true;
}
}
return false;
}
// V2
// (same as V1)
// https://leetcode.com/problems/increasing-triplet-subsequence/solutions/4462160/java-c-o-n-simple-easy-solution-step-by-step-explanation/
// V3
// IDEA : 2 POINTERS (out of memory error)
// public static boolean increasingTriplet_tp_3(int[] nums) {
// if (nums == null || nums.length < 3) {
// return false;
// }
//
// int len = nums.length;
// int[] leftMin = new int[len];
// leftMin[0] = nums[0];
// for (int i = 1; i < len; i++) {
// leftMin[i] = Math.min(leftMin[i - 1], nums[i]);
// }
//
// int[] rightMax = new int[len];
// rightMax[len - 1] = nums[len - 1];
// for (int i = len - 2; i >= 0; i--) {
// rightMax[i] = Math.max(rightMax[i + 1], nums[i]);
// }
//
// for (int i = 0; i < len; i++) {
// if (nums[i] > leftMin[i] && nums[i] < rightMax[i]) {
// return true;
// }
// }
//
// return false;
// }
}