Recognizing patterns in sequential data is a fundamental skill for any developer. This problem asks us to identify a specific "Up-Down-Up" shape within an array, which is a classic exercise in state management and pointer traversal.
Problem Summary
You're given:
An array of integers called nums with a length of .
Your goal:
Determine if the array is "trionic." This means the array must be divisible into three specific, contiguous parts:
- A strictly increasing sequence at the start.
- A strictly decreasing sequence in the middle.
- A strictly increasing sequence at the end.
Intuition
Think of a "trionic" array as a mountain followed by a valley. To validate this, we can act like a climber traversing the data from left to right. We need to pass through three distinct phases:
- The First Climb: We start at the first element and keep moving as long as the next number is larger than the current one. If we can't even take one step up, or if we reach the very end of the array without stopping, it's not trionic.
- The Descent: From the peak we just found, we must go down. We keep moving as long as the next number is smaller than the current one. If we don't move down at all, or if the descent takes us to the very last element (leaving no room for the final climb), the pattern fails.
- The Final Climb: From the bottom of the valley, we must climb again. We move forward as long as the numbers are increasing.
If, after these three phases, we find ourselves at the final index of the array, the array perfectly matches the trionic definition.
Walkthrough: Understanding the Examples
Example 1: `nums = [1, 3, 5, 4, 2, 6]`
- Phase 1 (Up): Start at 1. 3 is higher, 5 is higher. We stop at index 2 (value 5). This is our peak .
- Phase 2 (Down): From 5, 4 is lower, 2 is lower. We stop at index 4 (value 2). This is our valley .
- Phase 3 (Up): From 2, 6 is higher. We reach the end of the array at index 5.
- Result: True.
Example 2: `nums = [2, 1, 3]`
- Phase 1 (Up): Start at 2. The next number 1 is not higher. We cannot move. Since we are stuck at the start, the first segment is not strictly increasing.
- Result: False.
Code Blocks
C++
class Solution {
public:
bool isTrionic(vector<int>& nums) {
int n = nums.size();
int i = 0;
// Phase 1: Strictly Increasing
while (i + 1 < n && nums[i] < nums[i + 1]) {
i++;
}
// Must have moved, and must not be at the end
if (i == 0 || i == n - 1) return false;
// Phase 2: Strictly Decreasing
int peak = i;
while (i + 1 < n && nums[i] > nums[i + 1]) {
i++;
}
// Must have moved from the peak, and must not be at the end
if (i == peak || i == n - 1) return false;
// Phase 3: Strictly Increasing
while (i + 1 < n && nums[i] < nums[i + 1]) {
i++;
}
// Check if we reached the end of the array
return i == n - 1;
}
};
Python
class Solution:
def isTrionic(self, nums: list[int]) -> bool:
n = len(nums)
i = 0
# Phase 1: Strictly Increasing
while i + 1 < n and nums[i] < nums[i + 1]:
i += 1
if i == 0 or i == n - 1:
return False
# Phase 2: Strictly Decreasing
peak = i
while i + 1 < n and nums[i] > nums[i + 1]:
i += 1
if i == peak or i == n - 1:
return False
# Phase 3: Strictly Increasing
while i + 1 < n and nums[i] < nums[i + 1]:
i += 1
return i == n - 1
JavaScript
/**
* @param {number[]} nums
* @return {boolean}
*/
var isTrionic = function(nums) {
const n = nums.length;
let i = 0;
// Phase 1: Strictly Increasing
while (i + 1 < n && nums[i] < nums[i + 1]) {
i++;
}
if (i === 0 || i === n - 1) return false;
// Phase 2: Strictly Decreasing
let peak = i;
while (i + 1 < n && nums[i] > nums[i + 1]) {
i++;
}
if (i === peak || i === n - 1) return false;
// Phase 3: Strictly Increasing
while (i + 1 < n && nums[i] < nums[i + 1]) {
i++;
}
return i === n - 1;
};
Key Takeaways
- Linear Traversal: The solution runs in time because we only visit each element once.
-
State Management: By using a single index
ito track our progress through three distinct loops, we effectively manage the "state" of our climb without complex nested logic. -
Boundary Conditions: Checking
i == 0ori == n - 1is crucial to ensure that each segment actually exists and contains at least two numbers to form a slope.
Final Thoughts
This problem is an excellent introduction to "Mountain Array" variations. In real-world software engineering, similar logic is used in Signal Processing to identify peaks and valleys in sensor data or in Financial Analysis to detect specific market trends (like a "Head and Shoulders" pattern). Mastering the ability to walk through an array while validating specific conditions is a core skill for any technical interview.
Top comments (0)