↓Skip to main content
  1. LeetCode/

LeetCode 3637: Trionic Array I

·2 mins· ·
LeetCode Easy Array
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: easy First Attempt: 2025-08-03

  • Submissions: Wrong Answer → Runtime Error ×2 → Wrong Answer ×2 → Accepted; rewritten 2026-02-03: Wrong Answer → Accepted

Approach
#

Advance one index run by run (Array Template 1): strictly uphill to p, strictly downhill to q, then strictly uphill again. It holds when all three runs are non-empty (0 < p < q < n - 1) and the third run ends exactly at the last element.

My Attempts
#

VersionProblem
Attempt 1Added sentinels at both ends, but the index started at 0 and compared with nums[-1] (the trailing inf sentinel), so the first run counted as empty. [0, 5, 2, 3]: answer True, it returned False
Two REsSwitched to enumerating p, but the pasted code lost self and its indentation
Attempt 4return False was indented inside the for loop: only the first p was tried, and when the loop finished via continue nothing was returned, e.g. [0, 1, 3, 2] returned None
Attempt 5Moved return False out, but the downhill run could reach the last element, so an empty third run still passed. [2, 5, 2, 2]: answer False, it returned True
ACThree nested loops enumerating p and q and checking each run (n <= 100, so O(n³) passes)
Rewrite half a year laterTried a single scan but never required a final uphill run. [2, 5, 0]: answer False, it returned True

Solution
#

class Solution:
    def isTrionic(self, nums: List[int]) -> bool:
        n, i = len(nums), 0
        while i + 1 < n and nums[i] < nums[i + 1]:
            i += 1
        p = i
        while i + 1 < n and nums[i] > nums[i + 1]:
            i += 1
        q = i
        while i + 1 < n and nums[i] < nums[i + 1]:
            i += 1
        return 0 < p < q < n - 1 and i == n - 1

Complexity
#

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

Mistakes to Avoid
#

  • All three runs must be non-empty: p > 0, q > p, q < n - 1.
  • Confirm the third run reaches the end.
  • Put return False outside the loop.

Interview Explanation
#

I walk one index through uphill, downhill and uphill, then check all three runs are non-empty and the third ends exactly at the end of the array. Time O(n).

Related

LeetCode 1304: Find N Unique Integers Sum up to Zero
·1 min
LeetCode Easy Math Array
LeetCode 1304 note: place 1..n/2 and their negatives so each pair cancels, adding 0 when n is odd
LeetCode 2094: Finding 3-Digit Even Numbers
·2 mins
LeetCode Easy Hash-Table Enumeration Array
LeetCode 2094 note: enumerate the even numbers 100 to 998 instead and check that each digit needed is available; Counter subtraction keeps only positives
LeetCode 2176: Count Equal and Divisible Pairs in an Array
·1 min
LeetCode Easy Array
LeetCode 2176 note: n is only 100, so enumerate all pairs; the condition is equal values and the index product i * j divisible by k
LeetCode 1431: Kids With the Greatest Number of Candies
·1 min
LeetCode Easy Array
LeetCode 1431 note: compute the maximum once; a kid is True when their candies plus the extra reach >= the maximum, since a tie counts
LeetCode 3487: Maximum Unique Subarray Sum After Deletion
·2 mins
LeetCode Easy Greedy Hash-Table Array
LeetCode 3487 note: free deletion means picking distinct values; take every non-negative one, or only the maximum when all are negative
LeetCode 3507: Minimum Pair Removal to Sort Array I
·2 mins
LeetCode Easy Simulation Array
LeetCode 3507 note: n is tiny, so simulate; the right side of a slice assignment must be a list