Skip to content

Commit 05da8f0

Browse files
authored
feat: add solutions to lc problems: No.3105,3106 (#2550)
* No.3105.Longest Strictly Increasing or Strictly Decreasing Subarray * No.3106.Lexicographically Smallest String After Operations With Constraint
1 parent d7593ed commit 05da8f0

File tree

14 files changed

+551
-16
lines changed

14 files changed

+551
-16
lines changed

solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README.md

+105-4
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,125 @@
7373

7474
## 解法
7575

76-
### 方法一
76+
### 方法一:两次遍历
77+
78+
我们先进行一次遍历,找出严格递增的最长子数组长度,更新答案。然后再进行一次遍历,找出严格递减的最长子数组长度,再次更新答案。
79+
80+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
7781

7882
<!-- tabs:start -->
7983

8084
```python
81-
85+
class Solution:
86+
def longestMonotonicSubarray(self, nums: List[int]) -> int:
87+
ans = t = 1
88+
for i, x in enumerate(nums[1:]):
89+
if nums[i] < x:
90+
t += 1
91+
ans = max(ans, t)
92+
else:
93+
t = 1
94+
t = 1
95+
for i, x in enumerate(nums[1:]):
96+
if nums[i] > x:
97+
t += 1
98+
ans = max(ans, t)
99+
else:
100+
t = 1
101+
return ans
82102
```
83103

84104
```java
85-
105+
class Solution {
106+
public int longestMonotonicSubarray(int[] nums) {
107+
int ans = 1;
108+
for (int i = 1, t = 1; i < nums.length; ++i) {
109+
if (nums[i - 1] < nums[i]) {
110+
ans = Math.max(ans, ++t);
111+
} else {
112+
t = 1;
113+
}
114+
}
115+
for (int i = 1, t = 1; i < nums.length; ++i) {
116+
if (nums[i - 1] > nums[i]) {
117+
ans = Math.max(ans, ++t);
118+
} else {
119+
t = 1;
120+
}
121+
}
122+
return ans;
123+
}
124+
}
86125
```
87126

88127
```cpp
89-
128+
class Solution {
129+
public:
130+
int longestMonotonicSubarray(vector<int>& nums) {
131+
int ans = 1;
132+
for (int i = 1, t = 1; i < nums.size(); ++i) {
133+
if (nums[i - 1] < nums[i]) {
134+
ans = max(ans, ++t);
135+
} else {
136+
t = 1;
137+
}
138+
}
139+
for (int i = 1, t = 1; i < nums.size(); ++i) {
140+
if (nums[i - 1] > nums[i]) {
141+
ans = max(ans, ++t);
142+
} else {
143+
t = 1;
144+
}
145+
}
146+
return ans;
147+
}
148+
};
90149
```
91150
92151
```go
152+
func longestMonotonicSubarray(nums []int) int {
153+
ans := 1
154+
t := 1
155+
for i, x := range nums[1:] {
156+
if nums[i] < x {
157+
t++
158+
ans = max(ans, t)
159+
} else {
160+
t = 1
161+
}
162+
}
163+
t = 1
164+
for i, x := range nums[1:] {
165+
if nums[i] > x {
166+
t++
167+
ans = max(ans, t)
168+
} else {
169+
t = 1
170+
}
171+
}
172+
return ans
173+
}
174+
```
93175

176+
```ts
177+
function longestMonotonicSubarray(nums: number[]): number {
178+
let ans = 1;
179+
for (let i = 1, t = 1; i < nums.length; ++i) {
180+
if (nums[i - 1] < nums[i]) {
181+
ans = Math.max(ans, ++t);
182+
} else {
183+
t = 1;
184+
}
185+
}
186+
for (let i = 1, t = 1; i < nums.length; ++i) {
187+
if (nums[i - 1] > nums[i]) {
188+
ans = Math.max(ans, ++t);
189+
} else {
190+
t = 1;
191+
}
192+
}
193+
return ans;
194+
}
94195
```
95196

96197
<!-- tabs:end -->

solution/3100-3199/3105.Longest Strictly Increasing or Strictly Decreasing Subarray/README_EN.md

+105-4
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,125 @@
6767

6868
## Solutions
6969

70-
### Solution 1
70+
### Solution 1: Two Passes
71+
72+
We first perform a pass to find the length of the longest strictly increasing subarray, and update the answer. Then we perform another pass to find the length of the longest strictly decreasing subarray, and update the answer again.
73+
74+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
7175

7276
<!-- tabs:start -->
7377

7478
```python
75-
79+
class Solution:
80+
def longestMonotonicSubarray(self, nums: List[int]) -> int:
81+
ans = t = 1
82+
for i, x in enumerate(nums[1:]):
83+
if nums[i] < x:
84+
t += 1
85+
ans = max(ans, t)
86+
else:
87+
t = 1
88+
t = 1
89+
for i, x in enumerate(nums[1:]):
90+
if nums[i] > x:
91+
t += 1
92+
ans = max(ans, t)
93+
else:
94+
t = 1
95+
return ans
7696
```
7797

7898
```java
79-
99+
class Solution {
100+
public int longestMonotonicSubarray(int[] nums) {
101+
int ans = 1;
102+
for (int i = 1, t = 1; i < nums.length; ++i) {
103+
if (nums[i - 1] < nums[i]) {
104+
ans = Math.max(ans, ++t);
105+
} else {
106+
t = 1;
107+
}
108+
}
109+
for (int i = 1, t = 1; i < nums.length; ++i) {
110+
if (nums[i - 1] > nums[i]) {
111+
ans = Math.max(ans, ++t);
112+
} else {
113+
t = 1;
114+
}
115+
}
116+
return ans;
117+
}
118+
}
80119
```
81120

82121
```cpp
83-
122+
class Solution {
123+
public:
124+
int longestMonotonicSubarray(vector<int>& nums) {
125+
int ans = 1;
126+
for (int i = 1, t = 1; i < nums.size(); ++i) {
127+
if (nums[i - 1] < nums[i]) {
128+
ans = max(ans, ++t);
129+
} else {
130+
t = 1;
131+
}
132+
}
133+
for (int i = 1, t = 1; i < nums.size(); ++i) {
134+
if (nums[i - 1] > nums[i]) {
135+
ans = max(ans, ++t);
136+
} else {
137+
t = 1;
138+
}
139+
}
140+
return ans;
141+
}
142+
};
84143
```
85144
86145
```go
146+
func longestMonotonicSubarray(nums []int) int {
147+
ans := 1
148+
t := 1
149+
for i, x := range nums[1:] {
150+
if nums[i] < x {
151+
t++
152+
ans = max(ans, t)
153+
} else {
154+
t = 1
155+
}
156+
}
157+
t = 1
158+
for i, x := range nums[1:] {
159+
if nums[i] > x {
160+
t++
161+
ans = max(ans, t)
162+
} else {
163+
t = 1
164+
}
165+
}
166+
return ans
167+
}
168+
```
87169

170+
```ts
171+
function longestMonotonicSubarray(nums: number[]): number {
172+
let ans = 1;
173+
for (let i = 1, t = 1; i < nums.length; ++i) {
174+
if (nums[i - 1] < nums[i]) {
175+
ans = Math.max(ans, ++t);
176+
} else {
177+
t = 1;
178+
}
179+
}
180+
for (let i = 1, t = 1; i < nums.length; ++i) {
181+
if (nums[i - 1] > nums[i]) {
182+
ans = Math.max(ans, ++t);
183+
} else {
184+
t = 1;
185+
}
186+
}
187+
return ans;
188+
}
88189
```
89190

90191
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int longestMonotonicSubarray(vector<int>& nums) {
4+
int ans = 1;
5+
for (int i = 1, t = 1; i < nums.size(); ++i) {
6+
if (nums[i - 1] < nums[i]) {
7+
ans = max(ans, ++t);
8+
} else {
9+
t = 1;
10+
}
11+
}
12+
for (int i = 1, t = 1; i < nums.size(); ++i) {
13+
if (nums[i - 1] > nums[i]) {
14+
ans = max(ans, ++t);
15+
} else {
16+
t = 1;
17+
}
18+
}
19+
return ans;
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func longestMonotonicSubarray(nums []int) int {
2+
ans := 1
3+
t := 1
4+
for i, x := range nums[1:] {
5+
if nums[i] < x {
6+
t++
7+
ans = max(ans, t)
8+
} else {
9+
t = 1
10+
}
11+
}
12+
t = 1
13+
for i, x := range nums[1:] {
14+
if nums[i] > x {
15+
t++
16+
ans = max(ans, t)
17+
} else {
18+
t = 1
19+
}
20+
}
21+
return ans
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int longestMonotonicSubarray(int[] nums) {
3+
int ans = 1;
4+
for (int i = 1, t = 1; i < nums.length; ++i) {
5+
if (nums[i - 1] < nums[i]) {
6+
ans = Math.max(ans, ++t);
7+
} else {
8+
t = 1;
9+
}
10+
}
11+
for (int i = 1, t = 1; i < nums.length; ++i) {
12+
if (nums[i - 1] > nums[i]) {
13+
ans = Math.max(ans, ++t);
14+
} else {
15+
t = 1;
16+
}
17+
}
18+
return ans;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def longestMonotonicSubarray(self, nums: List[int]) -> int:
3+
ans = t = 1
4+
for i, x in enumerate(nums[1:]):
5+
if nums[i] < x:
6+
t += 1
7+
ans = max(ans, t)
8+
else:
9+
t = 1
10+
t = 1
11+
for i, x in enumerate(nums[1:]):
12+
if nums[i] > x:
13+
t += 1
14+
ans = max(ans, t)
15+
else:
16+
t = 1
17+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function longestMonotonicSubarray(nums: number[]): number {
2+
let ans = 1;
3+
for (let i = 1, t = 1; i < nums.length; ++i) {
4+
if (nums[i - 1] < nums[i]) {
5+
ans = Math.max(ans, ++t);
6+
} else {
7+
t = 1;
8+
}
9+
}
10+
for (let i = 1, t = 1; i < nums.length; ++i) {
11+
if (nums[i - 1] > nums[i]) {
12+
ans = Math.max(ans, ++t);
13+
} else {
14+
t = 1;
15+
}
16+
}
17+
return ans;
18+
}

0 commit comments

Comments
 (0)