-
Notifications
You must be signed in to change notification settings - Fork 43
/
longest-continuous-increasing-subsequence.py
209 lines (189 loc) · 5.81 KB
/
longest-continuous-increasing-subsequence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""
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.
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].
Example 1:
Input: nums = [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5] with length 3.
Even though [1,3,5,7] is an increasing subsequence, it is not continuous as elements 5 and 7 are separated by element
4.
Example 2:
Input: nums = [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2] with length 1. Note that it must be strictly
increasing.
Constraints:
1 <= nums.length <= 104
-109 <= nums[i] <= 109
"""
# V0
# IDEA : BRUTE FORCE (GREEDY) + 2 POINTERS
class Solution:
def findLengthOfLCIS(self, nums):
if len(nums) <= 1:
return len(nums)
r = 0
for i in range(len(nums)):
### Note : j starts from i + 1 and end at len(nums)
for j in range(i+1, len(nums)):
# if element in j > element in j -1 => increasing r = max(r, j-i+1)
if nums[j] > nums[j-1]:
# note : we need substring length, so we need j-i+1
r = max(r, j-i+1)
else:
# if not increasing, break, start from next i
break
# for dealing with the [1,1,1] case (i.e. the length should 1 rather than 0 in this case)
return max(r, 1)
# V0'
# IDEA : GREEDY
# DEMO : CHECK BELOW CASES
# nums = [1,2,3,4,2,5,6,7]
# elment = 1
# -> if we start from 1 and go from 1, 2... 4, then meet 2 and stop, since "1, 2, 3, 4, 2" not a sub-increase string
# -> but WE DON'T HAVE TO START FROM 2 (the 2nd element in the nums), SINCE THE sub-string from 2 is not going to "LONGER" THAN ABOVE ANYWAY
# so we just have to start from 2 instead
# elment = 2 (the second "2" in the nums)
# ..
# ..
class Solution:
def findLengthOfLCIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 0:
return 0
count = 1
result = 1
for i in range(1, len(nums)):
if nums[i] > nums[i - 1]:
count += 1
else:
result = max(result, count)
count = 1
result = max(result, count)
return result
# V0'
# IDEA : GREEDY
class Solution(object):
def findLengthOfLCIS(self, nums):
if not nums: return 0
count = 1
for i in range(len(nums)-1):
tmp_count = 1
j = i
while j < len(nums) - 1:
#print ("i :", i, "j :", j, "nums[j] :", nums[j], "nums[j+1] :", nums[j+1], "count :", count, "tmp_count :", tmp_count)
if nums[j+1] > nums[j]:
j += 1
tmp_count += 1
else:
break
count = max(count, tmp_count)
return count
# V0''
# IDEA : DP
class Solution(object):
def findLengthOfLCIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums: return 0
N = len(nums)
dp = [1] * N
for i in range(1, N):
if nums[i] > nums[i - 1]:
dp[i] = dp[i - 1] + 1
return max(dp)
# V1
# http://bookshadow.com/weblog/2017/09/10/leetcode-longest-continuous-increasing-subsequence/
# https://www.polarxiong.com/archives/LeetCode-674-longest-continuous-increasing-subsequence.html
# IDEA : GREEDY
class Solution(object):
def findLengthOfLCIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
ans = cnt = 0
last = None
for n in nums:
if n > last:
cnt += 1
else:
ans = max(ans, cnt)
cnt = 1
last = n
return max(ans, cnt)
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/79220527
# IDEA : DP
class Solution(object):
def findLengthOfLCIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums: return 0
N = len(nums)
dp = [1] * N
for i in range(1, N):
if nums[i] > nums[i - 1]:
dp[i] = dp[i - 1] + 1
return max(dp)
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/79220527
# IDEA : DP WITH SPACE OPTIMIZATION
class Solution(object):
def findLengthOfLCIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
longest = 0
cur = 0
for i in range(len(nums)):
if i == 0 or nums[i] > nums[i - 1]:
cur += 1
longest = max(longest, cur)
else:
cur = 1
return longest
# V1'''
# https://blog.csdn.net/fuxuemingzhu/article/details/79220527
class Solution(object):
def findLengthOfLCIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums: return 0
N = len(nums)
dp = 1
res = 1
for i in range(1, N):
if nums[i] > nums[i - 1]:
dp += 1
res = max(res, dp)
else:
dp = 1
return res
# V2
# Time: O(n)
# Space: O(1)
class Solution(object):
def findLengthOfLCIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
result, count = 0, 0
for i in range(len(nums)):
if i == 0 or nums[i-1] < nums[i]:
count += 1
result = max(result, count)
else:
count = 1
return result