Skip to content

Commit 2d07de1

Browse files
Added longest increasing subsequence algorithm (not requirement to be contiguous), will become better at doing tests before adding it to algorithm list. After exam I will try to put some focus on doing extensive testing on the algorithms I've currently implemented as well
1 parent a97031b commit 2d07de1

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
O(n^2) algorithm, can be faster and done in O(nlogn), but this works ok.
3+
To do: Create extensive test cases before adding to algorithm list.
4+
5+
'''
6+
7+
8+
def longest_increasing_subsequence(nums):
9+
if len(nums) == 0:
10+
return 0
11+
12+
OPT = [1 for i in range(len(nums))]
13+
14+
for i in range(1, len(nums)):
15+
for j in range(0, i):
16+
if nums[j] < nums[i] and OPT[j] + 1 > OPT[i]:
17+
OPT[i] = OPT[j] + 1
18+
19+
return max(OPT)
20+
21+
22+
if __name__ == '__main__':
23+
# test1 = [1,5,-2,10, 50, -10, 10, 1,2,3,4]
24+
test2 = [10, 1, 2, 11, 3, 5]
25+
# test3 = [10,9,8,5,3,2,1,2,3]
26+
# test4 = [1,5,2,3,4,5,6]
27+
test5 = []
28+
29+
print(test2)
30+
print(longest_increasing_subsequence(test2))

0 commit comments

Comments
 (0)