Skip to content

Commit c6502f5

Browse files
authored
feat: add solutions to lc problem: No.0674 (#2047)
No.0674.Longest Continuous Increasing Subsequence
1 parent f4c3c33 commit c6502f5

File tree

9 files changed

+431
-204
lines changed

9 files changed

+431
-204
lines changed

solution/0600-0699/0674.Longest Continuous Increasing Subsequence/README.md

+181-74
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,21 @@
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45-
设 f(i) 表示将数组第 i 项作为最长连续递增子序列的最后一项时,子序列的长度。
45+
**方法一:一次遍历**
4646

47-
那么,当 `nums[i - 1] < nums[i]`,即 `f(i) = f(i - 1)` + 1,否则 `f(i) = 1`。问题转换为求 f(i) (`i ∈ [0 ,n - 1]`) 的最大值
47+
我们可以遍历数组 $nums$,用变量 $cnt$ 记录当前连续递增序列的长度。初始时 $cnt = 1$
4848

49-
由于 f(i) 只与前一项 f(i - 1) 有关联,故不需要用一个数组存储。
49+
然后,我们从下标 $i = 1$ 开始,向右遍历数组 $nums$。每次遍历时,如果 $nums[i - 1] < nums[i]$,则说明当前元素可以加入到连续递增序列中,因此令 $cnt = cnt + 1$,然后更新答案为 $ans = \max(ans, cnt)$。否则,说明当前元素无法加入到连续递增序列中,因此令 $cnt = 1$。
50+
51+
遍历结束后,返回答案 $ans$ 即可。
52+
53+
时间复杂度 $O(n)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。
54+
55+
**方法二:双指针**
56+
57+
我们也可以用双指针 $i$ 和 $j$ 找到每一段连续递增序列,找出最长的连续递增序列的长度作为答案。
58+
59+
时间复杂度 $O(n)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。
5060

5161
<!-- tabs:start -->
5262

@@ -57,26 +67,28 @@
5767
```python
5868
class Solution:
5969
def findLengthOfLCIS(self, nums: List[int]) -> int:
60-
res, n = 1, len(nums)
61-
i = 0
62-
while i < n:
63-
j = i + 1
64-
while j < n and nums[j] > nums[j - 1]:
65-
j += 1
66-
res = max(res, j - i)
67-
i = j
68-
return res
70+
ans = cnt = 1
71+
for i, x in enumerate(nums[1:]):
72+
if nums[i] < x:
73+
cnt += 1
74+
ans = max(ans, cnt)
75+
else:
76+
cnt = 1
77+
return ans
6978
```
7079

7180
```python
7281
class Solution:
7382
def findLengthOfLCIS(self, nums: List[int]) -> int:
74-
n = len(nums)
75-
res = f = 1
76-
for i in range(1, n):
77-
f = 1 + (f if nums[i - 1] < nums[i] else 0)
78-
res = max(res, f)
79-
return res
83+
ans, n = 1, len(nums)
84+
i = 0
85+
while i < n:
86+
j = i + 1
87+
while j < n and nums[j - 1] < nums[j]:
88+
j += 1
89+
ans = max(ans, j - i)
90+
i = j
91+
return ans
8092
```
8193

8294
### **Java**
@@ -86,31 +98,33 @@ class Solution:
8698
```java
8799
class Solution {
88100
public int findLengthOfLCIS(int[] nums) {
89-
int res = 1;
90-
for (int i = 1, f = 1; i < nums.length; ++i) {
91-
f = 1 + (nums[i - 1] < nums[i] ? f : 0);
92-
res = Math.max(res, f);
101+
int ans = 1;
102+
for (int i = 1, cnt = 1; i < nums.length; ++i) {
103+
if (nums[i - 1] < nums[i]) {
104+
ans = Math.max(ans, ++cnt);
105+
} else {
106+
cnt = 1;
107+
}
93108
}
94-
return res;
109+
return ans;
95110
}
96111
}
97112
```
98113

99-
双指针:
100-
101114
```java
102115
class Solution {
103116
public int findLengthOfLCIS(int[] nums) {
104-
int res = 1;
105-
for (int i = 0, n = nums.length; i < n;) {
117+
int ans = 1;
118+
int n = nums.length;
119+
for (int i = 0; i < n;) {
106120
int j = i + 1;
107-
while (j < n && nums[j] > nums[j - 1]) {
121+
while (j < n && nums[j - 1] < nums[j]) {
108122
++j;
109123
}
110-
res = Math.max(res, j - i);
124+
ans = Math.max(ans, j - i);
111125
i = j;
112126
}
113-
return res;
127+
return ans;
114128
}
115129
}
116130
```
@@ -121,69 +135,139 @@ class Solution {
121135
class Solution {
122136
public:
123137
int findLengthOfLCIS(vector<int>& nums) {
124-
int res = 1;
125-
for (int i = 1, f = 1; i < nums.size(); ++i) {
126-
f = 1 + (nums[i - 1] < nums[i] ? f : 0);
127-
res = max(res, f);
138+
int ans = 1;
139+
for (int i = 1, cnt = 1; i < nums.size(); ++i) {
140+
if (nums[i - 1] < nums[i]) {
141+
ans = max(ans, ++cnt);
142+
} else {
143+
cnt = 1;
144+
}
128145
}
129-
return res;
146+
return ans;
130147
}
131148
};
132149
```
133150
134-
### **Rust**
135-
136-
```rust
137-
impl Solution {
138-
#[allow(dead_code)]
139-
pub fn find_length_of_lcis(nums: Vec<i32>) -> i32 {
140-
let n = nums.len();
141-
// Here dp[i] represents the longest lcis that ends with `nums[i]`, should be 1 by default
142-
let mut dp: Vec<i32> = vec![1; n];
143-
let mut ret = dp[0];
144-
145-
// Let's dp
146-
for i in 1..n {
147-
dp[i] = if nums[i] > nums[i - 1] { dp[i - 1] + 1 } else { 1 };
148-
ret = std::cmp::max(ret, dp[i]);
151+
```cpp
152+
class Solution {
153+
public:
154+
int findLengthOfLCIS(vector<int>& nums) {
155+
int ans = 1;
156+
int n = nums.size();
157+
for (int i = 0; i < n;) {
158+
int j = i + 1;
159+
while (j < n && nums[j - 1] < nums[j]) {
160+
++j;
161+
}
162+
ans = max(ans, j - i);
163+
i = j;
149164
}
150-
151-
ret
165+
return ans;
152166
}
153-
}
167+
};
154168
```
155169

156170
### **Go**
157171

158172
```go
159173
func findLengthOfLCIS(nums []int) int {
160-
res, f := 1, 1
161-
for i := 1; i < len(nums); i++ {
162-
if nums[i-1] < nums[i] {
163-
f += 1
164-
res = max(res, f)
174+
ans, cnt := 1, 1
175+
for i, x := range nums[1:] {
176+
if nums[i] < x {
177+
cnt++
178+
ans = max(ans, cnt)
165179
} else {
166-
f = 1
180+
cnt = 1
181+
}
182+
}
183+
return ans
184+
}
185+
```
186+
187+
```go
188+
func findLengthOfLCIS(nums []int) int {
189+
ans := 1
190+
n := len(nums)
191+
for i := 0; i < n; {
192+
j := i + 1
193+
for j < n && nums[j-1] < nums[j] {
194+
j++
167195
}
196+
ans = max(ans, j-i)
197+
i = j
168198
}
169-
return res
199+
return ans
200+
}
201+
```
202+
203+
### **Rust**
204+
205+
```rust
206+
impl Solution {
207+
pub fn find_length_of_lcis(nums: Vec<i32>) -> i32 {
208+
let mut ans = 1;
209+
let mut cnt = 1;
210+
for i in 1..nums.len() {
211+
if nums[i - 1] < nums[i] {
212+
ans = ans.max(cnt + 1);
213+
cnt += 1;
214+
} else {
215+
cnt = 1;
216+
}
217+
}
218+
ans
219+
}
220+
}
221+
```
222+
223+
```rust
224+
impl Solution {
225+
pub fn find_length_of_lcis(nums: Vec<i32>) -> i32 {
226+
let mut ans = 1;
227+
let n = nums.len();
228+
let mut i = 0;
229+
while i < n {
230+
let mut j = i + 1;
231+
while j < n && nums[j - 1] < nums[j] {
232+
j += 1;
233+
}
234+
ans = ans.max(j - i);
235+
i = j;
236+
}
237+
ans as i32
238+
}
170239
}
171240
```
172241

173242
### **TypeScript**
174243

175244
```ts
176245
function findLengthOfLCIS(nums: number[]): number {
246+
let [ans, cnt] = [1, 1];
247+
for (let i = 1; i < nums.length; ++i) {
248+
if (nums[i - 1] < nums[i]) {
249+
ans = Math.max(ans, ++cnt);
250+
} else {
251+
cnt = 1;
252+
}
253+
}
254+
return ans;
255+
}
256+
```
257+
258+
```ts
259+
function findLengthOfLCIS(nums: number[]): number {
260+
let ans = 1;
177261
const n = nums.length;
178-
let res = 1;
179-
let i = 0;
180-
for (let j = 1; j < n; j++) {
181-
if (nums[j - 1] >= nums[j]) {
182-
res = Math.max(res, j - i);
183-
i = j;
262+
for (let i = 0; i < n; ) {
263+
let j = i + 1;
264+
while (j < n && nums[j - 1] < nums[j]) {
265+
++j;
184266
}
267+
ans = Math.max(ans, j - i);
268+
i = j;
185269
}
186-
return Math.max(res, n - i);
270+
return ans;
187271
}
188272
```
189273

@@ -196,16 +280,39 @@ class Solution {
196280
* @return Integer
197281
*/
198282
function findLengthOfLCIS($nums) {
199-
$tmp = $max = 1;
200-
for ($i = 0; $i < count($nums) - 1; $i++) {
201-
if ($nums[$i] < $nums[$i + 1]) {
202-
$tmp++;
203-
$max = max($max, $tmp);
283+
$ans = 1;
284+
$cnt = 1;
285+
for ($i = 1; $i < count($nums); ++$i) {
286+
if ($nums[$i - 1] < $nums[$i]) {
287+
$ans = max($ans, ++$cnt);
204288
} else {
205-
$tmp = 1;
289+
$cnt = 1;
290+
}
291+
}
292+
return $ans;
293+
}
294+
}
295+
```
296+
297+
```php
298+
class Solution {
299+
/**
300+
* @param Integer[] $nums
301+
* @return Integer
302+
*/
303+
function findLengthOfLCIS($nums) {
304+
$ans = 1;
305+
$n = count($nums);
306+
$i = 0;
307+
while ($i < $n) {
308+
$j = $i + 1;
309+
while ($j < $n && $nums[$j - 1] < $nums[$j]) {
310+
$j++;
206311
}
312+
$ans = max($ans, $j - $i);
313+
$i = $j;
207314
}
208-
return $max;
315+
return $ans;
209316
}
210317
}
211318
```

0 commit comments

Comments
 (0)