- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Program to find length of longest increasing subsequence in Python

Suppose we have a list of numbers. We have to find the length of longest increasing subsequence. So if the input is like [6, 1, 7, 2, 8, 3, 4, 5], then the output will be 5, as the longest increasing subsequence is [2,3,4,5,6].

To solve this, we will follow these steps −

Make an array called tails whose size is same as nums, and fill this with 0.

size := 0

for each element x in nums array −

i := 0, j := size

while i is not same as j, then

mid := i + (j – i)/2

if tails[mid] < x, then i := mid + 1 otherwise j := mid

tails[i] := x

size := max ofi + 1 and size

return size.

Let us see the following implementation to get better understanding −

## Example

class Solution(object): def solve(self, nums): tails =[0 for i in range(len(nums))] size = 0 for x in nums: i=0 j=size while i!=j: mid = i + (j-i)//2 if tails[mid]> x: i= mid+1 else: j = mid tails[i] = x size = max(i+1,size) return size ob = Solution() nums = [7, 2, 8, 3, 9, 4, 5, 6] print(ob.solve(nums))

## Input

[7, 2, 8, 3, 9, 4, 5, 6]

## Output

5

- Related Articles
- Program to find length of longest circular increasing subsequence in python
- Program to find length of longest increasing subsequence with at least k odd values in Python
- Program to find length of longest balanced subsequence in Python
- Program to find length of longest anagram subsequence in Python
- Program to find length of longest palindromic subsequence in Python
- Program to find length of longest fibonacci subsequence in Python
- Longest Increasing Subsequence in Python
- Program to find length of longest consecutively increasing substring in Python
- Program to find length of longest common subsequence of three strings in Python
- Java Program for Longest Increasing Subsequence
- Longest Increasing Subsequence
- Program to find length of longest bitonic subsequence in C++
- Program to find length of longest common subsequence in C++
- Program to find out the length of longest palindromic subsequence using Python
- Program to find length of longest arithmetic subsequence with constant difference in Python

Advertisements