-
Notifications
You must be signed in to change notification settings - Fork 464
Expand file tree
/
Copy pathLongest_Increasing_Subsequence.py
More file actions
37 lines (28 loc) · 978 Bytes
/
Longest_Increasing_Subsequence.py
File metadata and controls
37 lines (28 loc) · 978 Bytes
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
# Solution 1
# O(n^2) time | O(n) space
def longestIncreasingSubsequence(array):
if len(array) <= 1:
return array
sequences = [None for _ in range(len(array))]
lengths = [1 for _ in range(len(array))]
maxLenIdx = 0
for i in range(len(array)):
curNum = array[i]
for j in range(i):
otherNum = array[j]
if otherNum < curNum and lengths[i] < lengths[j] + 1:
lengths[i] = lengths[j] + 1
sequences[i] = j
maxLenIdx = i
print("--------------")
print("lengths: ", lengths)
print("sequences: ", sequences)
print("maxLenIdx: ", maxLenIdx)
print("")
return buildSequence(array, sequences, maxLenIdx)
def buildSequence(nums, sequences, maxLenIdx):
result = [nums[maxLenIdx]]
while sequences[maxLenIdx] is not None:
maxLenIdx = sequences[maxLenIdx]
result.append(nums[maxLenIdx])
return list(reversed(result))