-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlongest_ascending.py
More file actions
executable file
·80 lines (69 loc) · 2.03 KB
/
longest_ascending.py
File metadata and controls
executable file
·80 lines (69 loc) · 2.03 KB
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
#!/usr/bin/python
# vim: foldlevel=0
"""
Given a sequence of numbers, write a function to find the length of a longest
increasing subsequence of the given sequence without the consecutive constraint.
"""
def lis(arr, j):
""" list(j) is the max length of seq ending at i, with arr[j] being part of it.
list(j) = 1 + max(lis(i)) if arr[i] < arr[j] else 1
"""
if j == 0:
return 1
curmax = 0
for i in range(j):
lis_i = lis(arr, i)
if arr[i] < arr[j] and lis_i+1 > curmax:
curmax = lis_i+1
return curmax
# Alternatively:
# return 1 + max([lis(arr, i) for i in range(j) if arr[i] < arr[j]] + [0])
def solution1(arr):
"""
Recursive.
>>> solution1([10, 22, 9, 33, 21, 50, 41, 60, 80])
6
>>> solution1([1, 3, -1])
2
>>> solution1([1, 3, 2, 4, 5, 1])
4
>>> solution1([1, 20, 5, 15, 40, 10, -5, -2])
4
"""
return max(lis(arr, i) for i in range(len(arr)))
def lis2(arr, j, memo):
"""
It's very easy to make mistakes here.
- You want to make sure that all max ascending subsequences for i in 0..j are
being computed. So watch for the if condition on line 7 (it has to be after
the recursive call to lis(i).
- Likewise don't try to make the code more concise. This won't work:
# return 1 + max([lis(X, i, memo) for i in range(j) if X[i] < X[j]] + [0])
"""
if memo[j] == -1:
curmax = 0
for i in range(j):
lis_i = lis2(arr, i, memo)
if arr[i] < arr[j] and lis_i+1 > curmax:
curmax = lis_i+1
memo[j] = curmax
return memo[j]
def solution2(arr):
"""
Dynamic programming. O(n^2)
>>> solution2([10, 22, 9, 33, 21, 50, 41, 60, 80])
6
>>> solution2([1, 3, -1])
2
>>> solution2([1, 3, 2, 4, 5, 1])
4
>>> solution2([1, 20, 5, 15, 40, 10, -5, -2])
4
"""
memo = [-1] * len(arr)
memo[0] = 1
lis2(arr, len(arr)-1, memo)
return max(memo)
if __name__ == "__main__":
import doctest
doctest.testmod()