Created
February 3, 2026 20:27
-
-
Save tatsuyax25/a88cdc8edd2f53ef393573c6847ce94e to your computer and use it in GitHub Desktop.
You are given an integer array nums of length n. An array is trionic if there exist indices 0 < p < q < n − 1 such that: nums[0...p] is strictly increasing,
nums[p...q] is strictly decreasing,
nums[q...n − 1] is strictly increasing.
Return true if
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} nums | |
| * @return {boolean} | |
| */ | |
| var isTrionic = function(nums) { | |
| const n = nums.length; | |
| // Need at least 4 elements to form inc → dec → inc | |
| if (n < 4) return false; | |
| let i = 0; | |
| // ------------------------------- | |
| // Phase 1: strictly increasing | |
| // ------------------------------- | |
| while (i + 1 < n && nums[i] < nums[i + 1]) { | |
| i++; | |
| } | |
| // If we never increased or we stopped at the last element, | |
| // we cannot form the required pattern. | |
| if (i === 0 || i === n - 1) return false; | |
| // ------------------------------- | |
| // Phase 2: strictly decreasing | |
| // ------------------------------- | |
| let peak = i; // p index | |
| while (i + 1 < n && nums[i] > nums[i + 1]) { | |
| i++; | |
| } | |
| // If we never decreased or we stopped at the last element, | |
| // we cannot form the required pattern. | |
| if (i === peak || i === n - 1) return false; | |
| // ------------------------------- | |
| // Phase 3: strictly increasing again | |
| // ------------------------------- | |
| let valley = i; // q index | |
| while (i + 1 < n && nums[i] < nums[i + 1]) { | |
| i++; | |
| } | |
| // If we never increased in this final phase, it's invalid. | |
| if (i === valley) return false; | |
| // If we consumed the entire array successfully, it's trionic. | |
| return i === n - 1; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment