Skip to content

Commit 1548e1a

Browse files
committed
✨ [334] O(n) solution for space
1 parent 5d4f56a commit 1548e1a

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

334/my_solution.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var increasingTriplet = function (nums) {
6+
let minToRight = [], maxToLeft = [];
7+
for (let i = 0; i < nums.length; i++) {
8+
minToRight[i] = Math.min((minToRight[i - 1] ?? Infinity), nums[i])
9+
}
10+
11+
for (let i = nums.length - 1; i >= 0; i--) {
12+
maxToLeft[i] = Math.max((maxToLeft[i + 1] ?? -Infinity), nums[i])
13+
}
14+
console.log('minToRight')
15+
console.log(minToRight)
16+
console.log("maxToLeft")
17+
console.log(maxToLeft)
18+
19+
for (let i = 0; i < nums.length; i++) {
20+
if (minToRight[i] < nums[i] && nums[i] < maxToLeft[i]) return true;
21+
}
22+
23+
return false;
24+
};
25+
26+
let x
27+
= increasingTriplet([1, 2, 3, 4, 5])
28+
29+
console.log("Result")
30+
console.log(x)

334/solution.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var increasingTriplet = function (nums) {
6+
let minToRight = [], maxToLeft = [];
7+
for (let i = 0; i < nums.length; i++) {
8+
minToRight[i] = Math.min((minToRight[i - 1] ?? Infinity), nums[i])
9+
}
10+
11+
for (let i = nums.length - 1; i >= 0; i--) {
12+
maxToLeft[i] = Math.max((maxToLeft[i + 1] ?? -Infinity), nums[i])
13+
}
14+
15+
for (let i = 0; i < nums.length; i++) {
16+
if (minToRight[i] < nums[i] && nums[i] < maxToLeft[i]) return true;
17+
}
18+
19+
return false;
20+
};

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
- [345. Reverse Vowels of a String](./345/)
7676
- [371. Sum of Two Integers](./371/)
7777
- [374. Top K Frequent Elements](./374/)
78+
- [334. Increasing Triplet Subsequence](./334/)
7879
- [338. Counting Bits](./338/)
7980
- [383. Ransom Note](./383/)
8081
- [394. Decode String](./394/)
@@ -157,7 +158,7 @@ Batch create:
157158
NOTE: JS IS HERE
158159
-->
159160
```ssh
160-
chapter=151 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
161+
chapter=334 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
161162
```
162163
> then you can use `x` for quick debug.
163164

0 commit comments

Comments
 (0)