From 436edf3a8821927cbe514af1aaf45cc03ad18b0d Mon Sep 17 00:00:00 2001 From: dhruvsaini Date: Tue, 3 Jan 2017 16:54:38 +0530 Subject: [PATCH] Create longest_increasing_subsequence.py The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order. For example, the length of LIS for {10, 22, 9, 33, 21, 50, 41, 60, 80} is 6 --- .../longest_increasing_subsequence.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 dynamic_programming/longest_increasing_subsequence.py diff --git a/dynamic_programming/longest_increasing_subsequence.py b/dynamic_programming/longest_increasing_subsequence.py new file mode 100644 index 000000000..37ddef207 --- /dev/null +++ b/dynamic_programming/longest_increasing_subsequence.py @@ -0,0 +1,12 @@ +""" +The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order. For example, the length of LIS for {10, 22, 9, 33, 21, 50, 41, 60, 80} is 6 +""" +def LIS(arr): + n= len(arr) + lis = [1]*n + + for i in range(1, n): + for j in range(0, i): + if arr[i] > arr[j] and lis[i] <= lis[j]: + lis[i] = lis[j] + 1 + return max(lis)