C#, O(n) one-pass, O(1) space, 140ms


  • 0
    H

    Basic idea: store potential singlet and doublets, replace them with better value when available.

    public class Solution {
    public bool IncreasingTriplet(int[] nums) {
        if(nums.Length < 3) return false;
        var singlet = Int32.MaxValue;
        var doublet = new int[]{Int32.MaxValue, Int32.MaxValue};
        for(int i = 0; i < nums.Length; i++)
        {
            if (nums[i] > doublet[1]) return true;
            else if(nums[i] > singlet)
            {
                doublet[0] = singlet;
                doublet[1] = nums[i];
            }
            else singlet = nums[i];
        }
        return false;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.