|
| 1 | +/* |
| 2 | +
|
| 3 | + -* Increasing Triplet Subsequence *- |
| 4 | +
|
| 5 | + 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. |
| 6 | +
|
| 7 | +
|
| 8 | +
|
| 9 | +Example 1: |
| 10 | +
|
| 11 | +Input: nums = [1,2,3,4,5] |
| 12 | +Output: true |
| 13 | +Explanation: Any triplet where i < j < k is valid. |
| 14 | +Example 2: |
| 15 | +
|
| 16 | +Input: nums = [5,4,3,2,1] |
| 17 | +Output: false |
| 18 | +Explanation: No triplet exists. |
| 19 | +Example 3: |
| 20 | +
|
| 21 | +Input: nums = [2,1,5,0,4,6] |
| 22 | +Output: true |
| 23 | +Explanation: The triplet (3, 4, 5) is valid because nums[3] == 0 < nums[4] == 4 < nums[5] == 6. |
| 24 | +
|
| 25 | +
|
| 26 | +Constraints: |
| 27 | +
|
| 28 | +1 <= nums.length <= 5 * 105 |
| 29 | +-231 <= nums[i] <= 231 - 1 |
| 30 | +
|
| 31 | +
|
| 32 | +Follow up: Could you implement a solution that runs in O(n) time complexity and O(1) space complexity? |
| 33 | +
|
| 34 | +*/ |
| 35 | +import 'dart:math'; |
| 36 | + |
| 37 | +class A { |
| 38 | +// Runtime: 468 ms, faster than 100.00% of Dart online submissions for Increasing Triplet Subsequence. |
| 39 | +// Memory Usage: 178.4 MB, less than 50.00% of Dart online submissions for Increasing Triplet Subsequence. |
| 40 | + bool increasingTriplet(List<int> nums) { |
| 41 | + int n = nums.length; |
| 42 | + |
| 43 | + // left_min[i] will store the minimum from left till ith |
| 44 | + |
| 45 | + List<int> leftMin = List.filled(n, 0); |
| 46 | + |
| 47 | + // right_max[i] will store the maximum from right till ith |
| 48 | + |
| 49 | + List<int> rightMax = List.filled(n, 0); |
| 50 | + |
| 51 | + // fill left_min array |
| 52 | + |
| 53 | + leftMin[0] = nums[0]; |
| 54 | + |
| 55 | + for (int i = 1; i < n; i++) { |
| 56 | + leftMin[i] = min(leftMin[i - 1], nums[i]); |
| 57 | + } |
| 58 | + |
| 59 | + // fill right_max array |
| 60 | + |
| 61 | + rightMax[n - 1] = nums[n - 1]; |
| 62 | + |
| 63 | + for (int i = n - 2; i >= 0; i--) { |
| 64 | + rightMax[i] = max(rightMax[i + 1], nums[i]); |
| 65 | + } |
| 66 | + |
| 67 | + // check that is there any element which has smaller element on left side and greater element on right side |
| 68 | + |
| 69 | + for (int i = 1; i < n - 1; i++) { |
| 70 | + if (leftMin[i - 1] < nums[i] && nums[i] < rightMax[i + 1]) { |
| 71 | + return true; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return false; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +class B { |
| 80 | +// Runtime: 749 ms, faster than 50.00% of Dart online submissions for Increasing Triplet Subsequence. |
| 81 | +// Memory Usage: 174.1 MB, less than 50.00% of Dart online submissions for Increasing Triplet Subsequence. |
| 82 | + bool increasingTriplet(List<int> nums) { |
| 83 | + int n = nums.length; |
| 84 | + |
| 85 | + // first will keep track of first element of triplet |
| 86 | + |
| 87 | + int first = double.maxFinite.toInt(); |
| 88 | + |
| 89 | + // second will keep track of second element of triple |
| 90 | + |
| 91 | + int second = double.maxFinite.toInt(); |
| 92 | + |
| 93 | + // second > first |
| 94 | + |
| 95 | + for (int i = 0; i < n; i++) { |
| 96 | + if (nums[i] <= first) { |
| 97 | + first = nums[i]; |
| 98 | + } else if (nums[i] <= second) { |
| 99 | + second = nums[i]; |
| 100 | + } else |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + return false; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +class C { |
| 109 | + // TLE O(n2) |
| 110 | + bool increasingTriplet(List<int> nums) { |
| 111 | + int n = nums.length; |
| 112 | + List<int> t = List.filled(n, 0); |
| 113 | + t[0] = 1; |
| 114 | + |
| 115 | + for (int i = 1; i < n; i++) { |
| 116 | + int temp = nums[i]; |
| 117 | + int ans = 0; |
| 118 | + for (int j = 0; j < i; j++) { |
| 119 | + if (nums[j] < temp) { |
| 120 | + ans = max(ans, t[j]); |
| 121 | + } |
| 122 | + } |
| 123 | + t[i] = 1 + ans; |
| 124 | + if (t[i] >= 3) return true; |
| 125 | + } |
| 126 | + |
| 127 | + for (int it in t) { |
| 128 | + if (it >= 3) { |
| 129 | + return true; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return false; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +class D { |
| 138 | + // TLE |
| 139 | + bool increasingTriplet(List<int> nums) { |
| 140 | + for (int i = 0; i < nums.length - 2; i++) { |
| 141 | + for (int j = i + 1; j < nums.length - 1; j++) { |
| 142 | + for (int k = j + 1; k < nums.length; k++) { |
| 143 | + if (nums[i] < nums[j] && nums[j] < nums[k]) { |
| 144 | + return true; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + return false; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +class E { |
| 154 | + // TLE |
| 155 | + bool increasingTriplet(List<int> nums) { |
| 156 | + List<int> prevSmallest = List.filled(nums.length, |
| 157 | + -1); // store first smallest value's index, initialize it with -1 |
| 158 | + List<int> nextLargest = List.filled(nums.length, |
| 159 | + -1); // store first largest value's index, initialize it with -1 |
| 160 | + |
| 161 | + for (int i = 1; i < nums.length; ++i) { |
| 162 | + // find first previous smallest value, traverse backward |
| 163 | + int j = i - 1; |
| 164 | + while (j >= 0) { |
| 165 | + if (nums[j] < nums[i]) { |
| 166 | + prevSmallest[i] = j; // Storing indexes |
| 167 | + break; |
| 168 | + } |
| 169 | + --j; |
| 170 | + } |
| 171 | + |
| 172 | + // find first next largest value, traverse backward |
| 173 | + j = i + 1; |
| 174 | + while (j < nums.length) { |
| 175 | + if (nums[i] < nums[j]) { |
| 176 | + nextLargest[i] = j; // Storing indexes |
| 177 | + } |
| 178 | + ++j; |
| 179 | + } |
| 180 | + |
| 181 | + // Check prev & next is not -1, compare values |
| 182 | + if ((prevSmallest[i] != -1 && nextLargest[i] != -1) && |
| 183 | + nums[prevSmallest[i]] < nums[i] && |
| 184 | + nums[i] < nums[nextLargest[i]]) return true; |
| 185 | + } |
| 186 | + |
| 187 | + return false; |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +class F { |
| 192 | +// Runtime: 630 ms, faster than 50.00% of Dart online submissions for Increasing Triplet Subsequence. |
| 193 | +// Memory Usage: 177.9 MB, less than 50.00% of Dart online submissions for Increasing Triplet Subsequence. |
| 194 | + bool increasingTriplet(List<int> nums) { |
| 195 | + List<int> prevSmallest = List.filled(nums.length, |
| 196 | + -1); // for storing first smallest value, initialize it with -1 |
| 197 | + List<int> nextLargest = List.filled(nums.length, |
| 198 | + -1); // for storing first largest value, initialize it with -1 |
| 199 | + |
| 200 | + prevSmallest[0] = nums[0]; // first element is smallest so far from start |
| 201 | + nextLargest[nums.length - 1] = |
| 202 | + nums[nums.length - 1]; // last element is largest so far from last |
| 203 | + |
| 204 | + for (int i = 1; i < nums.length; ++i) { |
| 205 | + prevSmallest[i] = min(prevSmallest[i - 1], |
| 206 | + nums[i]); // Store smallest value so far from start |
| 207 | + } |
| 208 | + |
| 209 | + for (int i = nums.length - 2; i >= 0; --i) { |
| 210 | + nextLargest[i] = max( |
| 211 | + nextLargest[i + 1], nums[i]); // Store largest value so far from last |
| 212 | + } |
| 213 | + |
| 214 | + for (int i = 1; i < nums.length - 1; ++i) { |
| 215 | + // Compare values |
| 216 | + if (prevSmallest[i] < nums[i] && nums[i] < nextLargest[i]) return true; |
| 217 | + } |
| 218 | + |
| 219 | + return false; |
| 220 | + } |
| 221 | +} |
0 commit comments