Skip to content

Commit 6657bb3

Browse files
authored
Create 334-increasing-triplet-subsequence.js
1 parent 750199e commit 6657bb3

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

334-increasing-triplet-subsequence.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
const increasingTriplet = function(nums) {
6+
// start with two largest values, as soon as we find a number bigger than both, while both have been updated, return true.
7+
let small = Number.MAX_VALUE, big = Number.MAX_VALUE;
8+
for (let n of nums) {
9+
if (n <= small) { small = n; } // update small if n is smaller than both
10+
else if (n <= big) { big = n; } // update big only if greater than small but smaller than big
11+
else return true; // return if you find a number bigger than both
12+
}
13+
return false;
14+
};

0 commit comments

Comments
 (0)