Skip to content

Commit 96daf25

Browse files
committed
Initial commit
1 parent 91816d9 commit 96daf25

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

longest_increasing_subsequence_pro.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Longest Increasing Subsequence
2+
# Given an integer array nums, return the length of the longest strictly increasing subsequence.
3+
# A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements.
4+
# For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7].
5+
# Input: nums = [10,9,2,5,3,7,101,18]
6+
# Output: 4
7+
# Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
8+
9+
def longest_increasing_subsequence(arr):
10+
cache = [1] * len(arr)
11+
for i in range(1, len(arr)):
12+
for j in range(i):
13+
if arr[i] > arr[j]:
14+
cache[i] = max(cache[i], cache[j] + 1)
15+
return max(cache)
16+
17+
18+
print(longest_increasing_subsequence(
19+
[0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3]))
20+
# 5

0 commit comments

Comments
 (0)