C# O(n) simple solution with comments


  • 0
    S
    public class Solution {
        public bool IncreasingTriplet(int[] nums) {
            int ith = Int32.MaxValue;
            int jth = Int32.MaxValue;
            
            for (int i = 0; i < nums.Length; i++){
                // Base contition, if nums[i] is greater than both ith and jth then simply return true
                if (nums[i] > ith && nums[i] > jth){
                    return true;
                }
                
                // if nums[i] is smaller than ith; then update ith because ith needs to be smalles of the three numbers
                if (nums[i] < ith){
                    ith = nums[i];
                    continue;
                }
                
                // If nums[i] is smaller than jth but greater than ith; then update jth because for kth to be bigger than 
                // jth, jth needs to be as small as possible
                if (nums[i] < jth && nums[i] > ith){
                    jth = nums[i];
                }
            }
            
            return false;
        }
    }
    

Log in to reply
 

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