Skip to content

Commit 92eac10

Browse files
authored
feat: add solutions to lc problem: No.3637 (#4618)
No.3637.Trionic Array I
1 parent b529cef commit 92eac10

File tree

7 files changed

+341
-8
lines changed

7 files changed

+341
-8
lines changed

solution/3600-3699/3637.Trionic Array I/README.md

Lines changed: 118 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,146 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3637.Tr
7373

7474
<!-- solution:start -->
7575

76-
### 方法一
76+
### 方法一:一次遍历
77+
78+
我们首先定义一个指针 $p$,初始时 $p = 0$,表示当前指向数组的第一个元素。我们将 $p$ 向右移动,直到找到第一个不满足严格递增的元素,即 $nums[p] \geq nums[p + 1]$。如果此时 $p = 0$,说明数组的前半部分没有严格递增的部分,因此直接返回 $\text{false}$。
79+
80+
接下来,我们定义另一个指针 $q$,初始时 $q = p$,表示当前指向数组的第二个部分的第一个元素。我们将 $q$ 向右移动,直到找到第一个不满足严格递减的元素,即 $nums[q] \leq nums[q + 1]$。如果此时 $q = p$ 或者 $q = n - 1$,说明数组的第二部分没有严格递减的部分或者没有第三部分,因此直接返回 $\text{false}$。
81+
82+
如果以上条件都满足,说明数组是三段式的,返回 $\text{true}$。
83+
84+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$,只使用了常数级别的额外空间。
7785

7886
<!-- tabs:start -->
7987

8088
#### Python3
8189

8290
```python
83-
91+
class Solution:
92+
def isTrionic(self, nums: List[int]) -> bool:
93+
n = len(nums)
94+
p = 0
95+
while p < n - 2 and nums[p] < nums[p + 1]:
96+
p += 1
97+
if p == 0:
98+
return False
99+
q = p
100+
while q < n - 1 and nums[q] > nums[q + 1]:
101+
q += 1
102+
if q == p or q == n - 1:
103+
return False
104+
while q < n - 1 and nums[q] < nums[q + 1]:
105+
q += 1
106+
return q == n - 1
84107
```
85108

86109
#### Java
87110

88111
```java
89-
112+
class Solution {
113+
public boolean isTrionic(int[] nums) {
114+
int n = nums.length;
115+
int p = 0;
116+
while (p < n - 2 && nums[p] < nums[p + 1]) {
117+
p++;
118+
}
119+
if (p == 0) {
120+
return false;
121+
}
122+
int q = p;
123+
while (q < n - 1 && nums[q] > nums[q + 1]) {
124+
q++;
125+
}
126+
if (q == p || q == n - 1) {
127+
return false;
128+
}
129+
while (q < n - 1 && nums[q] < nums[q + 1]) {
130+
q++;
131+
}
132+
return q == n - 1;
133+
}
134+
}
90135
```
91136

92137
#### C++
93138

94139
```cpp
95-
140+
class Solution {
141+
public:
142+
bool isTrionic(vector<int>& nums) {
143+
int n = nums.size();
144+
int p = 0;
145+
while (p < n - 2 && nums[p] < nums[p + 1]) {
146+
p++;
147+
}
148+
if (p == 0) {
149+
return false;
150+
}
151+
int q = p;
152+
while (q < n - 1 && nums[q] > nums[q + 1]) {
153+
q++;
154+
}
155+
if (q == p || q == n - 1) {
156+
return false;
157+
}
158+
while (q < n - 1 && nums[q] < nums[q + 1]) {
159+
q++;
160+
}
161+
return q == n - 1;
162+
}
163+
};
96164
```
97165
98166
#### Go
99167
100168
```go
169+
func isTrionic(nums []int) bool {
170+
n := len(nums)
171+
p := 0
172+
for p < n-2 && nums[p] < nums[p+1] {
173+
p++
174+
}
175+
if p == 0 {
176+
return false
177+
}
178+
q := p
179+
for q < n-1 && nums[q] > nums[q+1] {
180+
q++
181+
}
182+
if q == p || q == n-1 {
183+
return false
184+
}
185+
for q < n-1 && nums[q] < nums[q+1] {
186+
q++
187+
}
188+
return q == n-1
189+
}
190+
```
101191

192+
#### TypeScript
193+
194+
```ts
195+
function isTrionic(nums: number[]): boolean {
196+
const n = nums.length;
197+
let p = 0;
198+
while (p < n - 2 && nums[p] < nums[p + 1]) {
199+
p++;
200+
}
201+
if (p === 0) {
202+
return false;
203+
}
204+
let q = p;
205+
while (q < n - 1 && nums[q] > nums[q + 1]) {
206+
q++;
207+
}
208+
if (q === p || q === n - 1) {
209+
return false;
210+
}
211+
while (q < n - 1 && nums[q] < nums[q + 1]) {
212+
q++;
213+
}
214+
return q === n - 1;
215+
}
102216
```
103217

104218
<!-- tabs:end -->

solution/3600-3699/3637.Trionic Array I/README_EN.md

Lines changed: 118 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,146 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3637.Tr
7171

7272
<!-- solution:start -->
7373

74-
### Solution 1
74+
### Solution 1: Single Pass
75+
76+
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.
77+
78+
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.
79+
80+
If all the above conditions are satisfied, it means the array is trionic, and we return $\text{true}$.
81+
82+
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.
7583

7684
<!-- tabs:start -->
7785

7886
#### Python3
7987

8088
```python
81-
89+
class Solution:
90+
def isTrionic(self, nums: List[int]) -> bool:
91+
n = len(nums)
92+
p = 0
93+
while p < n - 2 and nums[p] < nums[p + 1]:
94+
p += 1
95+
if p == 0:
96+
return False
97+
q = p
98+
while q < n - 1 and nums[q] > nums[q + 1]:
99+
q += 1
100+
if q == p or q == n - 1:
101+
return False
102+
while q < n - 1 and nums[q] < nums[q + 1]:
103+
q += 1
104+
return q == n - 1
82105
```
83106

84107
#### Java
85108

86109
```java
87-
110+
class Solution {
111+
public boolean isTrionic(int[] nums) {
112+
int n = nums.length;
113+
int p = 0;
114+
while (p < n - 2 && nums[p] < nums[p + 1]) {
115+
p++;
116+
}
117+
if (p == 0) {
118+
return false;
119+
}
120+
int q = p;
121+
while (q < n - 1 && nums[q] > nums[q + 1]) {
122+
q++;
123+
}
124+
if (q == p || q == n - 1) {
125+
return false;
126+
}
127+
while (q < n - 1 && nums[q] < nums[q + 1]) {
128+
q++;
129+
}
130+
return q == n - 1;
131+
}
132+
}
88133
```
89134

90135
#### C++
91136

92137
```cpp
93-
138+
class Solution {
139+
public:
140+
bool isTrionic(vector<int>& nums) {
141+
int n = nums.size();
142+
int p = 0;
143+
while (p < n - 2 && nums[p] < nums[p + 1]) {
144+
p++;
145+
}
146+
if (p == 0) {
147+
return false;
148+
}
149+
int q = p;
150+
while (q < n - 1 && nums[q] > nums[q + 1]) {
151+
q++;
152+
}
153+
if (q == p || q == n - 1) {
154+
return false;
155+
}
156+
while (q < n - 1 && nums[q] < nums[q + 1]) {
157+
q++;
158+
}
159+
return q == n - 1;
160+
}
161+
};
94162
```
95163
96164
#### Go
97165
98166
```go
167+
func isTrionic(nums []int) bool {
168+
n := len(nums)
169+
p := 0
170+
for p < n-2 && nums[p] < nums[p+1] {
171+
p++
172+
}
173+
if p == 0 {
174+
return false
175+
}
176+
q := p
177+
for q < n-1 && nums[q] > nums[q+1] {
178+
q++
179+
}
180+
if q == p || q == n-1 {
181+
return false
182+
}
183+
for q < n-1 && nums[q] < nums[q+1] {
184+
q++
185+
}
186+
return q == n-1
187+
}
188+
```
99189

190+
#### TypeScript
191+
192+
```ts
193+
function isTrionic(nums: number[]): boolean {
194+
const n = nums.length;
195+
let p = 0;
196+
while (p < n - 2 && nums[p] < nums[p + 1]) {
197+
p++;
198+
}
199+
if (p === 0) {
200+
return false;
201+
}
202+
let q = p;
203+
while (q < n - 1 && nums[q] > nums[q + 1]) {
204+
q++;
205+
}
206+
if (q === p || q === n - 1) {
207+
return false;
208+
}
209+
while (q < n - 1 && nums[q] < nums[q + 1]) {
210+
q++;
211+
}
212+
return q === n - 1;
213+
}
100214
```
101215

102216
<!-- tabs:end -->
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
bool isTrionic(vector<int>& nums) {
4+
int n = nums.size();
5+
int p = 0;
6+
while (p < n - 2 && nums[p] < nums[p + 1]) {
7+
p++;
8+
}
9+
if (p == 0) {
10+
return false;
11+
}
12+
int q = p;
13+
while (q < n - 1 && nums[q] > nums[q + 1]) {
14+
q++;
15+
}
16+
if (q == p || q == n - 1) {
17+
return false;
18+
}
19+
while (q < n - 1 && nums[q] < nums[q + 1]) {
20+
q++;
21+
}
22+
return q == n - 1;
23+
}
24+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func isTrionic(nums []int) bool {
2+
n := len(nums)
3+
p := 0
4+
for p < n-2 && nums[p] < nums[p+1] {
5+
p++
6+
}
7+
if p == 0 {
8+
return false
9+
}
10+
q := p
11+
for q < n-1 && nums[q] > nums[q+1] {
12+
q++
13+
}
14+
if q == p || q == n-1 {
15+
return false
16+
}
17+
for q < n-1 && nums[q] < nums[q+1] {
18+
q++
19+
}
20+
return q == n-1
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public boolean isTrionic(int[] nums) {
3+
int n = nums.length;
4+
int p = 0;
5+
while (p < n - 2 && nums[p] < nums[p + 1]) {
6+
p++;
7+
}
8+
if (p == 0) {
9+
return false;
10+
}
11+
int q = p;
12+
while (q < n - 1 && nums[q] > nums[q + 1]) {
13+
q++;
14+
}
15+
if (q == p || q == n - 1) {
16+
return false;
17+
}
18+
while (q < n - 1 && nums[q] < nums[q + 1]) {
19+
q++;
20+
}
21+
return q == n - 1;
22+
}
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def isTrionic(self, nums: List[int]) -> bool:
3+
n = len(nums)
4+
p = 0
5+
while p < n - 2 and nums[p] < nums[p + 1]:
6+
p += 1
7+
if p == 0:
8+
return False
9+
q = p
10+
while q < n - 1 and nums[q] > nums[q + 1]:
11+
q += 1
12+
if q == p or q == n - 1:
13+
return False
14+
while q < n - 1 and nums[q] < nums[q + 1]:
15+
q += 1
16+
return q == n - 1

0 commit comments

Comments
 (0)