FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm-python2/longest_increasing_sequence.py at master · yangjun616/algorithm-python2 · GitHub
yangjun616
algorithm-python2
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm-python2
/
longest_increasing_sequence.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
31 lines (28 loc) · 731 Bytes
Breadcrumbs
algorithm-python2
/
longest_increasing_sequence.py
Copy path
File metadata and controls
31 lines (28 loc) · 731 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/python
#coding: utf-8
def
isBiggerCompare
(
a
,
b
):
return
a
>
b
def
findLIS
(
sequence
,
compare
=
isBiggerCompare
):
n
=
len
(
sequence
)
dp
=
[
0
for
i
in
range
(
n
)]
track
=
[
-
1
for
i
in
range
(
n
)]
ans
=
1
for
i
in
range
(
1
,
n
):
MAX
=
0
for
j
in
range
(
i
):
if
compare
(
sequence
[
i
],
sequence
[
j
])
and
MAX
<
dp
[
j
]:
MAX
=
dp
[
j
]
track
[
i
]
=
j
# track[i] sequence[i]作为最长上升序列末项的前一项
dp
[
i
]
=
MAX
+
1
# dp[i] 以sequence[i]结尾的最长上升序列长度
if
dp
[
i
]
>
dp
[
ans
]:
ans
=
i
ansList
=
[
sequence
[
ans
]]
while
track
[
ans
]
!=
-
1
:
ans
=
track
[
ans
]
ansList
.
insert
(
0
,
sequence
[
ans
])
return
ansList
if
__name__
==
'__main__'
:
s
=
[
3
,
1
,
2
,
1
,
4
,
3
,
5
]
print
findLIS
(
s
)
Back
|
FazBrowse Home
|
New Git URL