Created
April 9, 2020 23:39
-
-
Save RP-3/694cc93f8d00463768d1feccb727cd0c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} nums | |
* @return {boolean} | |
*/ | |
// O(n) | |
// Idea: track the lowest and highest number so far. | |
// If you find a number higher than the highest so | |
// far, you've found a triplet sequence! | |
var increasingTriplet = function(nums) { | |
if(nums.length < 3) return false; | |
let [s, m] = [Infinity, Infinity]; | |
for(let i=0; i<nums.length; i++){ | |
if(nums[i] > m) return true; | |
if(nums[i] <= s) s = nums[i]; | |
else if(nums[i] <= m) m = nums[i]; | |
} | |
return false; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment