Skip to content

Commit 7b43853

Browse files
committed
Longest continuous increasing subsequence.
1 parent a30d513 commit 7b43853

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
Given an unsorted array of integers nums, return the length of the longest continuous increasing subsequence (i.e. subarray). The subsequence must be strictly increasing.
3+
4+
A continuous increasing subsequence is defined by two indices l and r (l < r) such that it is [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]] and for each l <= i < r, nums[i] < nums[i + 1].
5+
6+
 
7+
8+
Example 1:
9+
Input: nums = [1,3,5,4,7]
10+
Output: 3
11+
Explanation: The longest continuous increasing subsequence is [1,3,5] with length 3.
12+
Even though [1,3,5,7] is an increasing subsequence, it is not continuous as elements 5 and 7 are separated by element
13+
4.
14+
15+
Example 2:
16+
Input: nums = [2,2,2,2,2]
17+
Output: 1
18+
Explanation: The longest continuous increasing subsequence is [2] with length 1. Note that it must be strictly
19+
increasing.
20+
 
21+
22+
Constraints:
23+
- 1 <= nums.length <= 10^4
24+
- -109 <= nums[i] <= 10^9
25+
*/
26+
class Solution {
27+
func findLengthOfLCIS(_ nums: [Int]) -> Int {
28+
var i = 0
29+
var max = 0
30+
var current = 0
31+
while i < nums.count - 1 {
32+
for j in i..<nums.count - 1 {
33+
if nums[j] < nums[j + 1] {
34+
current += 1
35+
} else {
36+
if current > max {
37+
max = current
38+
}
39+
break
40+
}
41+
}
42+
i += (current > 0 ? current : 1)
43+
if current > max {
44+
max = current
45+
}
46+
current = 0
47+
}
48+
if current > max {
49+
max = current
50+
}
51+
return max + 1
52+
}
53+
}
54+
55+
let s = Solution()
56+
let r = s.findLengthOfLCIS([2,2,2,2,2])
57+
print(r)
58+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/674.Longest Continuous Increasing Subsequence.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
116. [Maximum Average Subarray I](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/643.Maximum%20Average%20Subarray%20I.playground/Contents.swift)
121121
117. [Set Mismatch](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/645.Set%20Mismatch.playground/Contents.swift)
122122
118. [Robot Return to Origin](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/657.Robot%20Return%20to%20Origin.playground/Contents.swift)
123+
119. [Longest Continuous Increasing Subsequence](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/674.Longest%20Continuous%20Increasing%20Subsequence.Robot%20Return%20to%20Origin.playground/Contents.swift)
123124

124125
#### Medium
125126

0 commit comments

Comments
 (0)