-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathincreasing_triplet_subsequence.dart
221 lines (169 loc) · 5.6 KB
/
increasing_triplet_subsequence.dart
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
/*
-* Increasing Triplet Subsequence *-
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.
Example 1:
Input: nums = [1,2,3,4,5]
Output: true
Explanation: Any triplet where i < j < k is valid.
Example 2:
Input: nums = [5,4,3,2,1]
Output: false
Explanation: No triplet exists.
Example 3:
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.
Constraints:
1 <= nums.length <= 5 * 105
-231 <= nums[i] <= 231 - 1
Follow up: Could you implement a solution that runs in O(n) time complexity and O(1) space complexity?
*/
import 'dart:math';
class A {
// Runtime: 468 ms, faster than 100.00% of Dart online submissions for Increasing Triplet Subsequence.
// Memory Usage: 178.4 MB, less than 50.00% of Dart online submissions for Increasing Triplet Subsequence.
bool increasingTriplet(List<int> nums) {
int n = nums.length;
// left_min[i] will store the minimum from left till ith
List<int> leftMin = List.filled(n, 0);
// right_max[i] will store the maximum from right till ith
List<int> rightMax = List.filled(n, 0);
// fill left_min array
leftMin[0] = nums[0];
for (int i = 1; i < n; i++) {
leftMin[i] = min(leftMin[i - 1], nums[i]);
}
// fill right_max array
rightMax[n - 1] = nums[n - 1];
for (int i = n - 2; i >= 0; i--) {
rightMax[i] = max(rightMax[i + 1], nums[i]);
}
// check that is there any element which has smaller element on left side and greater element on right side
for (int i = 1; i < n - 1; i++) {
if (leftMin[i - 1] < nums[i] && nums[i] < rightMax[i + 1]) {
return true;
}
}
return false;
}
}
class B {
// Runtime: 749 ms, faster than 50.00% of Dart online submissions for Increasing Triplet Subsequence.
// Memory Usage: 174.1 MB, less than 50.00% of Dart online submissions for Increasing Triplet Subsequence.
bool increasingTriplet(List<int> nums) {
int n = nums.length;
// first will keep track of first element of triplet
int first = double.maxFinite.toInt();
// second will keep track of second element of triple
int second = double.maxFinite.toInt();
// second > first
for (int i = 0; i < n; i++) {
if (nums[i] <= first) {
first = nums[i];
} else if (nums[i] <= second) {
second = nums[i];
} else
return true;
}
return false;
}
}
class C {
// TLE O(n2)
bool increasingTriplet(List<int> nums) {
int n = nums.length;
List<int> t = List.filled(n, 0);
t[0] = 1;
for (int i = 1; i < n; i++) {
int temp = nums[i];
int ans = 0;
for (int j = 0; j < i; j++) {
if (nums[j] < temp) {
ans = max(ans, t[j]);
}
}
t[i] = 1 + ans;
if (t[i] >= 3) return true;
}
for (int it in t) {
if (it >= 3) {
return true;
}
}
return false;
}
}
class D {
// TLE
bool increasingTriplet(List<int> nums) {
for (int i = 0; i < nums.length - 2; i++) {
for (int j = i + 1; j < nums.length - 1; j++) {
for (int k = j + 1; k < nums.length; k++) {
if (nums[i] < nums[j] && nums[j] < nums[k]) {
return true;
}
}
}
}
return false;
}
}
class E {
// TLE
bool increasingTriplet(List<int> nums) {
List<int> prevSmallest = List.filled(nums.length,
-1); // store first smallest value's index, initialize it with -1
List<int> nextLargest = List.filled(nums.length,
-1); // store first largest value's index, initialize it with -1
for (int i = 1; i < nums.length; ++i) {
// find first previous smallest value, traverse backward
int j = i - 1;
while (j >= 0) {
if (nums[j] < nums[i]) {
prevSmallest[i] = j; // Storing indexes
break;
}
--j;
}
// find first next largest value, traverse backward
j = i + 1;
while (j < nums.length) {
if (nums[i] < nums[j]) {
nextLargest[i] = j; // Storing indexes
}
++j;
}
// Check prev & next is not -1, compare values
if ((prevSmallest[i] != -1 && nextLargest[i] != -1) &&
nums[prevSmallest[i]] < nums[i] &&
nums[i] < nums[nextLargest[i]]) return true;
}
return false;
}
}
class F {
// Runtime: 630 ms, faster than 50.00% of Dart online submissions for Increasing Triplet Subsequence.
// Memory Usage: 177.9 MB, less than 50.00% of Dart online submissions for Increasing Triplet Subsequence.
bool increasingTriplet(List<int> nums) {
List<int> prevSmallest = List.filled(nums.length,
-1); // for storing first smallest value, initialize it with -1
List<int> nextLargest = List.filled(nums.length,
-1); // for storing first largest value, initialize it with -1
prevSmallest[0] = nums[0]; // first element is smallest so far from start
nextLargest[nums.length - 1] =
nums[nums.length - 1]; // last element is largest so far from last
for (int i = 1; i < nums.length; ++i) {
prevSmallest[i] = min(prevSmallest[i - 1],
nums[i]); // Store smallest value so far from start
}
for (int i = nums.length - 2; i >= 0; --i) {
nextLargest[i] = max(
nextLargest[i + 1], nums[i]); // Store largest value so far from last
}
for (int i = 1; i < nums.length - 1; ++i) {
// Compare values
if (prevSmallest[i] < nums[i] && nums[i] < nextLargest[i]) return true;
}
return false;
}
}