func increasingTriplet(nums []int) bool {
low := int(math.Exp2(31))
high := int(math.Exp2(31))
for _, num := range nums {
if num <= low {
low = num
} else if num <= high {
high = num
} else {
return true
}
}
return false
}
-
Make use of two variables to keep track of first and second number in triplet.
-
Traverse through the array keep track of
low
andhigh
. On encountering a new minimum, updatelow
. On encountering a minimum number lesserhigh
but higher thanlow
, updatehigh
. -
This way, we encounter a third number given
low
andhigh
have been set and a triplet is formed.