-
Notifications
You must be signed in to change notification settings - Fork 43
/
longest-increasing-subsequence.py
334 lines (283 loc) · 9.16 KB
/
longest-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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"""
300. Longest Increasing Subsequence
Medium
11275
Given an integer array nums, return the length of the longest strictly increasing subsequence.
A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7].
Example 1:
Input: nums = [10,9,2,5,3,7,101,18]
Output: 4
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
Example 2:
Input: nums = [0,1,0,3,2,3]
Output: 4
Example 3:
Input: nums = [7,7,7,7,7,7,7]
Output: 1
Constraints:
1 <= nums.length <= 2500
-104 <= nums[i] <= 104
Follow up: Can you come up with an algorithm that runs in O(n log(n)) time complexity?
"""
# V0
# IDEA : DP
# DP equation :
# -> dp[i] = max(1, dp[j] + 1), while i > j and nums[i] > nums[i]
# -> dp[0] = 1
# dp[idx] : the longest lengh of the sub-array at index = idx
# PROCESS :
# Start from dp[0], then go through all nums
# and update dp on the same time
# then return max(dp) which is the longest lengh of the sub-array
# time complexity : O(n^2), space complexity : O(n)
class Solution:
def lengthOfLIS(self, nums):
# write your code here
if nums is None or not nums:
return 0
dp = [1] * len(nums)
for i in range(len(nums)):
for j in range(i):
"""
NOTE !!! we ONLY need to deal with "if nums[i] > nums[j]" condition
"""
if nums[i] > nums[j]:
dp[i] = max(dp[i], dp[j]+1)
return max(dp)
# V0'
# IDEA : ARRAY
class Solution:
def lengthOfLIS(self, nums):
sub = [nums[0]]
for num in nums[1:]:
if num > sub[-1]:
sub.append(num)
else:
# Find the first element in sub that is greater than or equal to num
i = 0
while num > sub[i]:
i += 1
sub[i] = num
return len(sub)
# V0''
# IDEA : DP
# time complexity : O(n^2), space complexity : O(n)
class Solution(object):
def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums: return 0
dp = [0] * len(nums)
dp[0] = 1
for i in range(1, len(nums)):
tmax = 1
for j in range(0, i):
if nums[i] > nums[j]:
tmax = max(tmax, dp[j] + 1)
dp[i] = tmax
return max(dp)
# V0'''
# IDEA : improve With Binary Search
# https://leetcode.com/problems/longest-increasing-subsequence/solution/
class Solution:
def lengthOfLIS(self, nums):
sub = []
for num in nums:
i = bisect_left(sub, num)
# If num is greater than any element in sub
if i == len(sub):
sub.append(num)
# Otherwise, replace the first element in sub greater than or equal to num
else:
sub[i] = num
return len(sub)
# V1
# IDEA : DP
# https://leetcode.com/problems/longest-increasing-subsequence/solution/
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
dp = [1] * len(nums)
for i in range(1, len(nums)):
for j in range(i):
if nums[i] > nums[j]:
dp[i] = max(dp[i], dp[j] + 1)
return max(dp)
# V1
# IDEA : Intelligently Build a Subsequence
# https://leetcode.com/problems/longest-increasing-subsequence/solution/
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
sub = [nums[0]]
for num in nums[1:]:
if num > sub[-1]:
sub.append(num)
else:
# Find the first element in sub that is greater than or equal to num
i = 0
while num > sub[i]:
i += 1
sub[i] = num
return len(sub)
# V1
# IDEA : improve With Binary Search
# https://leetcode.com/problems/longest-increasing-subsequence/solution/
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
sub = []
for num in nums:
i = bisect_left(sub, num)
# If num is greater than any element in sub
if i == len(sub):
sub.append(num)
# Otherwise, replace the first element in sub greater than or equal to num
else:
sub[i] = num
return len(sub)
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79820919
# IDEA : DP
class Solution(object):
def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums: return 0
dp = [0] * len(nums)
dp[0] = 1
for i in range(1, len(nums)):
tmax = 1
for j in range(0, i):
if nums[i] > nums[j]:
tmax = max(tmax, dp[j] + 1)
dp[i] = tmax
return max(dp)
# V1'
# https://www.jiuzhang.com/solution/longest-increasing-subsequence/#tag-highlight-lang-python
# IDEA : DP
class Solution:
"""
@param nums: The integer array
@return: The length of LIS (longest increasing subsequence)
"""
def longestIncreasingSubsequence(self, nums):
# write your code here
if nums is None or not nums:
return 0
dp = [1] * len(nums)
for curr, val in enumerate(nums):
for prev in range(curr):
if nums[prev] < val:
dp[curr] = max(dp[curr], dp[prev] + 1)
return max(dp)
# V1''
# https://www.jiuzhang.com/solution/longest-increasing-subsequence/#tag-highlight-lang-python
# IDEA : DP + binary search
# time complexity : O(nlogn), space complexity : O(n)
class Solution:
"""
@param nums: The integer array
@return: The length of LIS (longest increasing subsequence)
"""
def longestIncreasingSubsequence(self, nums):
if nums is None or not nums:
return 0
# state: dp[i] : longest length of sequence from left to right till i
# initialization: dp[0..n-1] = 1
dp = [1] * len(nums)
# prev[i] : the best solution of dp[i] is from which dp[j]
prev = [-1] * len(nums)
# function dp[i] = max{dp[j] + 1}, j < i and nums[j] < nums[i]
for i in range(len(nums)):
for j in range(i):
if nums[j] < nums[i] and dp[i] < dp[j] + 1:
dp[i] = dp[j] + 1
prev[i] = j
# answer: max(dp[0..n-1])
longest, last = 0, -1
for i in range(len(nums)):
if dp[i] > longest:
longest = dp[i]
last = i
path = []
while last != -1:
path.append(nums[last])
last = prev[last]
print(path[::-1])
return longest
# V1'''
# https://www.jiuzhang.com/solution/longest-increasing-subsequence/#tag-highlight-lang-python
class Solution:
"""
@param nums: An integer array
@return: The length of LIS (longest increasing subsequence)
"""
def longestIncreasingSubsequence(self, nums):
if nums is None or len(nums) == 0:
return 0
n = len(nums)
f = [0] * n
g = [0] * (n + 1)
lis = f[0] = 1
g[1] = nums[0]
for i in range(1, n):
if nums[i] > g[lis]:
f[i] = lis
lis += 1
g[lis] = nums[i]
else:
l, r = 1, lis
while l != r:
mid = (l + r) // 2
if g[mid] < nums[i]:
l = mid + 1
else:
r = mid
f[i] = l
g[l] = min(g[l], nums[i])
return lis
# V2
# Time: O(nlogn)
# Space: O(n)
class Solution(object):
def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
LIS = []
def insert(target):
left, right = 0, len(LIS) - 1
# Find the first index "left" which satisfies LIS[left] >= target
while left <= right:
mid = left + (right - left) // 2
if LIS[mid] >= target:
right = mid - 1
else:
left = mid + 1
# If not found, append the target.
if left == len(LIS):
LIS.append(target)
else:
LIS[left] = target
for num in nums:
insert(num)
return len(LIS)
# Time: O(n^2)
# Space: O(n)
# Traditional DP solution.
class Solution2(object):
def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dp = [] # dp[i]: the length of LIS ends with nums[i]
for i in range(len(nums)):
dp.append(1)
for j in range(i):
if nums[j] < nums[i]:
dp[i] = max(dp[i], dp[j] + 1)
return max(dp) if dp else 0