Skip to content

Instantly share code, notes, and snippets.

@RP-3
Created April 9, 2020 23:39
Show Gist options
  • Save RP-3/694cc93f8d00463768d1feccb727cd0c to your computer and use it in GitHub Desktop.
Save RP-3/694cc93f8d00463768d1feccb727cd0c to your computer and use it in GitHub Desktop.
/**
* @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