Skip to content

feat: add solutions to lc problem: No.3637 #4618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 118 additions & 4 deletions solution/3600-3699/3637.Trionic Array I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,32 +73,146 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3637.Tr

<!-- solution:start -->

### 方法一
### 方法一:一次遍历

我们首先定义一个指针 $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)$,只使用了常数级别的额外空间。

<!-- tabs:start -->

#### 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<int>& 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;
}
```

<!-- tabs:end -->
Expand Down
122 changes: 118 additions & 4 deletions solution/3600-3699/3637.Trionic Array I/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,32 +71,146 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3637.Tr

<!-- solution:start -->

### 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.

<!-- tabs:start -->

#### 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<int>& 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;
}
```

<!-- tabs:end -->
Expand Down
24 changes: 24 additions & 0 deletions solution/3600-3699/3637.Trionic Array I/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
bool isTrionic(vector<int>& 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;
}
};
21 changes: 21 additions & 0 deletions solution/3600-3699/3637.Trionic Array I/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 23 additions & 0 deletions solution/3600-3699/3637.Trionic Array I/Solution.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
16 changes: 16 additions & 0 deletions solution/3600-3699/3637.Trionic Array I/Solution.py
Original file line number Diff line number Diff line change
@@ -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
Loading