334.go 322 B

1234567891011121314151617181920
  1. func increasingTriplet(nums []int) bool {
  2. n := len(nums)
  3. if n < 3 {
  4. return false
  5. }
  6. st := []int{nums[0]}
  7. top := 0
  8. for i := 1; top < 2 && i < n; i++ {
  9. if num := nums[i]; st[top] < num {
  10. st = append(st, num)
  11. top++
  12. } else if num <= st[0] {
  13. st[0] = num
  14. } else {
  15. st[1] = num
  16. }
  17. }
  18. return top == 2
  19. }