[LeetCode] Trionic Array I

3637. Trionic Array I

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 nums is trionic, otherwise return false.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

class Solution {
public:
bool isTrionic(vector<int>& nums) {
long long n = nums.size();
vector<array<long long,3>> inc;
for(int i = 0; i < n - 1; i++) {
int j = i + 1;
if(nums[j] <= nums[i]) continue;
inc.push_back({i,i,nums[i]});
while(j < n and nums[j] > nums[j-1]) {
inc.back()[1] = j;
inc.back()[2] += nums[j];
j++;
}
i = j - 1;
}
if(inc.size() != 2) return false;
auto dec = [&](long long l, long long r) {
for(int i = l; i < r; i++) {
if(nums[i] <= nums[i+1]) return false;
}
return true;
};
return inc[0][0] == 0 and inc[1][1] == n - 1 and dec(inc[0][1], inc[1][0]);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/08/03/PS/LeetCode/trionic-array-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.