-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path300. Longest Increasing Subsequence.py
45 lines (40 loc) · 1.36 KB
/
300. 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
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 23 13:25:50 2018
@author: Wenchang Chen
"""
def lengthOfLIS(nums):
def getlength(nums, calbool):
if len(nums) == 1:
return 1
if len(nums) == 0:
return 0
lislength = []
for idx, num in enumerate(nums):
if calbool[idx] == 0:
continue
print('nums', nums)
print('idx', idx)
print('num', num)
print('cal' , calbool)
print('----------------------')
if idx == len(nums) - 1:
continue
nums2 = nums[idx+1:]
lislength2 = [1] * len(nums2)
flag = 0
small = float('Inf')
for idx2, num2 in enumerate(nums2):
if num2 > num:
calbool[idx2+idx+1] = 0
if num2 < small:
small = num2
flag = 1
lislength2[idx2] += getlength(nums2[idx2:], [1] + [0] * (len(nums2[idx2:]) - 1))
elif flag == 0 and sum(calbool) > 1:
calbool[idx2+idx+1] = 1
break
lislength += [max(lislength2)]
print(lislength)
return max(lislength)
return getlength(nums, [1]*len(nums))