↓Skip to main content
  1. LeetCode/

LeetCode 334: Increasing Triplet Subsequence

·2 mins· ·
LeetCode Medium Array Greedy Longest-Increasing-Subsequence
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-11-17

  • Submissions: Time Limit Exceeded → Runtime Error → Accepted → Wrong Answer

Approach
#

Greedy (Greedy): first is the smallest value so far, and second is the smallest value that has some smaller value before it. For each x: if x <= first update first; else if x <= second update second; else x > second completes a triplet. Updating first leaves second valid, since a smaller value already preceded it. Compare with <=; equal values are not increasing.

My Attempts
#

  • The first 2022 version enumerated three indices recursively and timed out exponentially. Switching to the first/second greedy above passed.
  • A 2025 rewrite used if first > num: first, second = num, first and elif second > num:
    • With strict >, a value equal to first fell to the next branch and became second: [0, 0, 1] returned True instead of False.
    • Shifting the old first into second is also wrong in itself: the old first need not have a smaller value before it.

Solution
#

class Solution:
    def increasingTriplet(self, nums: List[int]) -> bool:
        first = second = float("inf")
        for x in nums:
            if x <= first:
                first = x
            elif x <= second:
                second = x
            else:
                return True
        return False

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Use <=: a value equal to first cannot serve as second.
  • Do not shift the old first into second when updating first.
  • Enumerating three indices is O(n³).

Interview Explanation
#

I keep first, the smallest value so far, and second, the smallest value with something smaller before it; a new value updates first, else second, and anything larger than second completes the triplet. Time O(n), space O(1).

Related

LeetCode 324: Wiggle Sort II
·2 mins
LeetCode Medium Array Divide-and-Conquer Greedy Sorting
LeetCode 324 note: sort, split into a small half and a large half, reverse each, and interleave with small values at even indices and large ones at odd indices
LeetCode 435: Non-overlapping Intervals
·2 mins
LeetCode Medium Array Dynamic-Programming Greedy Sorting
LeetCode 435 note: minimum removals = total minus the maximum kept; sort by right end and greedily keep the one ending earliest
LeetCode 134: Gas Station
·2 mins
LeetCode Medium Array Greedy
LeetCode 134 note: if total gas is short there is no answer; otherwise scan, and whenever the tank goes negative move the start to the next station
LeetCode 11: Container With Most Water
·2 mins
LeetCode Medium Array Two-Pointers Greedy
LeetCode 11 note: start pointers at both ends and always move the shorter side; moving the taller side only narrows the width without raising the height
LeetCode 402: Remove K Digits
·2 mins
LeetCode Medium String Stack Greedy Monotonic-Stack
LeetCode 402 note: a monotonically increasing stack; while deletions remain, pop any top larger than the current digit, then strip leading zeros
LeetCode 313: Super Ugly Number
·2 mins
LeetCode Medium Array Math Dynamic-Programming
LeetCode 313 note: generalizing LC 264 with one pointer per prime; with a heap, avoid pushing the same number many times