leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 80364cba46622522bb7573ecbfd4bc246c172d70
parent 33da8f1d4dee4987f9836fbb170a9d6e62f38ce9
Author: Jack Sangdahl <jack@pngu.org>
Date:   Mon,  2 Feb 2026 21:41:18 -0700

3637. trionic array 1

Diffstat:
Atrionic_array1.c | 36++++++++++++++++++++++++++++++++++++
Atrionic_array1.py | 36++++++++++++++++++++++++++++++++++++
2 files changed, 72 insertions(+), 0 deletions(-)

diff --git a/trionic_array1.c b/trionic_array1.c @@ -0,0 +1,36 @@ +// 3637. Trionic Array I +#include "leetcode.h" + +/* + * 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, and 'nums[q ... n - 1]' + * is strictly increasing. return true if 'nums' is trionic, otherwise return + * false. + */ + +bool isTrionic(int *nums, int numsSize) { + if (numsSize < 4) + return false; + int trans[4][2] = {{1, 4}, {1, 2}, {3, 2}, {3, 4}}; + int state = 0; + for (int i = 1; i < numsSize; i++) { + if (nums[i] == nums[i - 1]) + return false; + int input = (nums[i] > nums[i - 1]) ? 0 : 1; + state = (state < 4) ? trans[state][input] : 4; + if (state == 4) + return false; + } + return state == 3; +} + +int main() { + int n1[] = {1, 3, 5, 4, 2, 6}, n2[] = {2, 1, 3}; + bool r1 = isTrionic(n1, ARRAY_SIZE(n1)); + bool r2 = isTrionic(n2, ARRAY_SIZE(n2)); + printf("%d\n", r1); // expect: 1 + assert(r1 == true); + printf("%d\n", r2); // expect: 0 + assert(r2 == false); +} diff --git a/trionic_array1.py b/trionic_array1.py @@ -0,0 +1,36 @@ +# 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, and 'nums[q ... n - 1]' +is strictly increasing. return true if 'nums' is trionic, otherwise return +false. +""" + + +class Solution(object): + def isTrionic(self, nums): + """ + :type nums: List[int] + :rtype: bool + """ + n, i = len(nums), 0 + while i + 1 < n and nums[i] < nums[i + 1]: + i += 1 + if i == 0 or i == n - 1: + return False + p = i + while i + 1 < n and nums[i] > nums[i + 1]: + i += 1 + if i == p or i == n - 1: + return False + while i + 1 < n and nums[i] < nums[i + 1]: + i += 1 + return i == n - 1 + + +if __name__ == "__main__": + obj = Solution() + print(obj.isTrionic(nums=[1, 3, 5, 4, 2, 6])) + print(obj.isTrionic(nums=[2, 1, 3]))