From b6c47b5a026f7ed0bb4d3750ca9fc7815bb98367 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 3 Aug 2025 21:07:36 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3637 No.3637.Trionic Array I --- .../3600-3699/3637.Trionic Array I/README.md | 122 +++++++++++++++++- .../3637.Trionic Array I/README_EN.md | 122 +++++++++++++++++- .../3637.Trionic Array I/Solution.cpp | 24 ++++ .../3637.Trionic Array I/Solution.go | 21 +++ .../3637.Trionic Array I/Solution.java | 23 ++++ .../3637.Trionic Array I/Solution.py | 16 +++ .../3637.Trionic Array I/Solution.ts | 21 +++ 7 files changed, 341 insertions(+), 8 deletions(-) create mode 100644 solution/3600-3699/3637.Trionic Array I/Solution.cpp create mode 100644 solution/3600-3699/3637.Trionic Array I/Solution.go create mode 100644 solution/3600-3699/3637.Trionic Array I/Solution.java create mode 100644 solution/3600-3699/3637.Trionic Array I/Solution.py create mode 100644 solution/3600-3699/3637.Trionic Array I/Solution.ts diff --git a/solution/3600-3699/3637.Trionic Array I/README.md b/solution/3600-3699/3637.Trionic Array I/README.md index 9769ed130208b..835b26d047c99 100644 --- a/solution/3600-3699/3637.Trionic Array I/README.md +++ b/solution/3600-3699/3637.Trionic Array I/README.md @@ -73,32 +73,146 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3637.Tr -### 方法一 +### 方法一:一次遍历 + +我们首先定义一个指针 $p$,初始时 $p = 0$,表示当前指向数组的第一个元素。我们将 $p$ 向右移动,直到找到第一个不满足严格递增的元素,即 $nums[p] \geq nums[p + 1]$。如果此时 $p = 0$,说明数组的前半部分没有严格递增的部分,因此直接返回 $\text{false}$。 + +接下来,我们定义另一个指针 $q$,初始时 $q = p$,表示当前指向数组的第二个部分的第一个元素。我们将 $q$ 向右移动,直到找到第一个不满足严格递减的元素,即 $nums[q] \leq nums[q + 1]$。如果此时 $q = p$ 或者 $q = n - 1$,说明数组的第二部分没有严格递减的部分或者没有第三部分,因此直接返回 $\text{false}$。 + +如果以上条件都满足,说明数组是三段式的,返回 $\text{true}$。 + +时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$,只使用了常数级别的额外空间。 #### Python3 ```python - +class Solution: + def isTrionic(self, nums: List[int]) -> bool: + n = len(nums) + p = 0 + while p < n - 2 and nums[p] < nums[p + 1]: + p += 1 + if p == 0: + return False + q = p + while q < n - 1 and nums[q] > nums[q + 1]: + q += 1 + if q == p or q == n - 1: + return False + while q < n - 1 and nums[q] < nums[q + 1]: + q += 1 + return q == n - 1 ``` #### Java ```java - +class Solution { + public boolean isTrionic(int[] nums) { + int n = nums.length; + int p = 0; + while (p < n - 2 && nums[p] < nums[p + 1]) { + p++; + } + if (p == 0) { + return false; + } + int q = p; + while (q < n - 1 && nums[q] > nums[q + 1]) { + q++; + } + if (q == p || q == n - 1) { + return false; + } + while (q < n - 1 && nums[q] < nums[q + 1]) { + q++; + } + return q == n - 1; + } +} ``` #### C++ ```cpp - +class Solution { +public: + bool isTrionic(vector& nums) { + int n = nums.size(); + int p = 0; + while (p < n - 2 && nums[p] < nums[p + 1]) { + p++; + } + if (p == 0) { + return false; + } + int q = p; + while (q < n - 1 && nums[q] > nums[q + 1]) { + q++; + } + if (q == p || q == n - 1) { + return false; + } + while (q < n - 1 && nums[q] < nums[q + 1]) { + q++; + } + return q == n - 1; + } +}; ``` #### Go ```go +func isTrionic(nums []int) bool { + n := len(nums) + p := 0 + for p < n-2 && nums[p] < nums[p+1] { + p++ + } + if p == 0 { + return false + } + q := p + for q < n-1 && nums[q] > nums[q+1] { + q++ + } + if q == p || q == n-1 { + return false + } + for q < n-1 && nums[q] < nums[q+1] { + q++ + } + return q == n-1 +} +``` +#### TypeScript + +```ts +function isTrionic(nums: number[]): boolean { + const n = nums.length; + let p = 0; + while (p < n - 2 && nums[p] < nums[p + 1]) { + p++; + } + if (p === 0) { + return false; + } + let q = p; + while (q < n - 1 && nums[q] > nums[q + 1]) { + q++; + } + if (q === p || q === n - 1) { + return false; + } + while (q < n - 1 && nums[q] < nums[q + 1]) { + q++; + } + return q === n - 1; +} ``` diff --git a/solution/3600-3699/3637.Trionic Array I/README_EN.md b/solution/3600-3699/3637.Trionic Array I/README_EN.md index aeae707e7a563..57f3219882e2c 100644 --- a/solution/3600-3699/3637.Trionic Array I/README_EN.md +++ b/solution/3600-3699/3637.Trionic Array I/README_EN.md @@ -71,32 +71,146 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3637.Tr -### Solution 1 +### Solution 1: Single Pass + +We first define a pointer $p$, initially $p = 0$, pointing to the first element of the array. We move $p$ to the right until we find the first element that doesn't satisfy strict increasing order, i.e., $nums[p] \geq nums[p + 1]$. If $p = 0$ at this point, it means the first part of the array doesn't have a strictly increasing section, so we return $\text{false}$ directly. + +Next, we define another pointer $q$, initially $q = p$, pointing to the first element of the second part of the array. We move $q$ to the right until we find the first element that doesn't satisfy strict decreasing order, i.e., $nums[q] \leq nums[q + 1]$. If $q = p$ or $q = n - 1$ at this point, it means the second part of the array doesn't have a strictly decreasing section or there's no third part, so we return $\text{false}$ directly. + +If all the above conditions are satisfied, it means the array is trionic, and we return $\text{true}$. + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$, using only constant extra space. #### Python3 ```python - +class Solution: + def isTrionic(self, nums: List[int]) -> bool: + n = len(nums) + p = 0 + while p < n - 2 and nums[p] < nums[p + 1]: + p += 1 + if p == 0: + return False + q = p + while q < n - 1 and nums[q] > nums[q + 1]: + q += 1 + if q == p or q == n - 1: + return False + while q < n - 1 and nums[q] < nums[q + 1]: + q += 1 + return q == n - 1 ``` #### Java ```java - +class Solution { + public boolean isTrionic(int[] nums) { + int n = nums.length; + int p = 0; + while (p < n - 2 && nums[p] < nums[p + 1]) { + p++; + } + if (p == 0) { + return false; + } + int q = p; + while (q < n - 1 && nums[q] > nums[q + 1]) { + q++; + } + if (q == p || q == n - 1) { + return false; + } + while (q < n - 1 && nums[q] < nums[q + 1]) { + q++; + } + return q == n - 1; + } +} ``` #### C++ ```cpp - +class Solution { +public: + bool isTrionic(vector& nums) { + int n = nums.size(); + int p = 0; + while (p < n - 2 && nums[p] < nums[p + 1]) { + p++; + } + if (p == 0) { + return false; + } + int q = p; + while (q < n - 1 && nums[q] > nums[q + 1]) { + q++; + } + if (q == p || q == n - 1) { + return false; + } + while (q < n - 1 && nums[q] < nums[q + 1]) { + q++; + } + return q == n - 1; + } +}; ``` #### Go ```go +func isTrionic(nums []int) bool { + n := len(nums) + p := 0 + for p < n-2 && nums[p] < nums[p+1] { + p++ + } + if p == 0 { + return false + } + q := p + for q < n-1 && nums[q] > nums[q+1] { + q++ + } + if q == p || q == n-1 { + return false + } + for q < n-1 && nums[q] < nums[q+1] { + q++ + } + return q == n-1 +} +``` +#### TypeScript + +```ts +function isTrionic(nums: number[]): boolean { + const n = nums.length; + let p = 0; + while (p < n - 2 && nums[p] < nums[p + 1]) { + p++; + } + if (p === 0) { + return false; + } + let q = p; + while (q < n - 1 && nums[q] > nums[q + 1]) { + q++; + } + if (q === p || q === n - 1) { + return false; + } + while (q < n - 1 && nums[q] < nums[q + 1]) { + q++; + } + return q === n - 1; +} ``` diff --git a/solution/3600-3699/3637.Trionic Array I/Solution.cpp b/solution/3600-3699/3637.Trionic Array I/Solution.cpp new file mode 100644 index 0000000000000..5f48fc7ca6fc0 --- /dev/null +++ b/solution/3600-3699/3637.Trionic Array I/Solution.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + bool isTrionic(vector& nums) { + int n = nums.size(); + int p = 0; + while (p < n - 2 && nums[p] < nums[p + 1]) { + p++; + } + if (p == 0) { + return false; + } + int q = p; + while (q < n - 1 && nums[q] > nums[q + 1]) { + q++; + } + if (q == p || q == n - 1) { + return false; + } + while (q < n - 1 && nums[q] < nums[q + 1]) { + q++; + } + return q == n - 1; + } +}; \ No newline at end of file diff --git a/solution/3600-3699/3637.Trionic Array I/Solution.go b/solution/3600-3699/3637.Trionic Array I/Solution.go new file mode 100644 index 0000000000000..359dd8cfb8be0 --- /dev/null +++ b/solution/3600-3699/3637.Trionic Array I/Solution.go @@ -0,0 +1,21 @@ +func isTrionic(nums []int) bool { + n := len(nums) + p := 0 + for p < n-2 && nums[p] < nums[p+1] { + p++ + } + if p == 0 { + return false + } + q := p + for q < n-1 && nums[q] > nums[q+1] { + q++ + } + if q == p || q == n-1 { + return false + } + for q < n-1 && nums[q] < nums[q+1] { + q++ + } + return q == n-1 +} diff --git a/solution/3600-3699/3637.Trionic Array I/Solution.java b/solution/3600-3699/3637.Trionic Array I/Solution.java new file mode 100644 index 0000000000000..3f8ad2284b89a --- /dev/null +++ b/solution/3600-3699/3637.Trionic Array I/Solution.java @@ -0,0 +1,23 @@ +class Solution { + public boolean isTrionic(int[] nums) { + int n = nums.length; + int p = 0; + while (p < n - 2 && nums[p] < nums[p + 1]) { + p++; + } + if (p == 0) { + return false; + } + int q = p; + while (q < n - 1 && nums[q] > nums[q + 1]) { + q++; + } + if (q == p || q == n - 1) { + return false; + } + while (q < n - 1 && nums[q] < nums[q + 1]) { + q++; + } + return q == n - 1; + } +} diff --git a/solution/3600-3699/3637.Trionic Array I/Solution.py b/solution/3600-3699/3637.Trionic Array I/Solution.py new file mode 100644 index 0000000000000..3a7aabd4788b6 --- /dev/null +++ b/solution/3600-3699/3637.Trionic Array I/Solution.py @@ -0,0 +1,16 @@ +class Solution: + def isTrionic(self, nums: List[int]) -> bool: + n = len(nums) + p = 0 + while p < n - 2 and nums[p] < nums[p + 1]: + p += 1 + if p == 0: + return False + q = p + while q < n - 1 and nums[q] > nums[q + 1]: + q += 1 + if q == p or q == n - 1: + return False + while q < n - 1 and nums[q] < nums[q + 1]: + q += 1 + return q == n - 1 diff --git a/solution/3600-3699/3637.Trionic Array I/Solution.ts b/solution/3600-3699/3637.Trionic Array I/Solution.ts new file mode 100644 index 0000000000000..5aed4ee50e8eb --- /dev/null +++ b/solution/3600-3699/3637.Trionic Array I/Solution.ts @@ -0,0 +1,21 @@ +function isTrionic(nums: number[]): boolean { + const n = nums.length; + let p = 0; + while (p < n - 2 && nums[p] < nums[p + 1]) { + p++; + } + if (p === 0) { + return false; + } + let q = p; + while (q < n - 1 && nums[q] > nums[q + 1]) { + q++; + } + if (q === p || q === n - 1) { + return false; + } + while (q < n - 1 && nums[q] < nums[q + 1]) { + q++; + } + return q === n - 1; +}