Skip to main content
Back to problems
Leetcode
Medium
Arrays
Dynamic Programming
Binary Search
Google
Meta
Amazon
Longest Increasing Subsequence

Find the length of the longest strictly increasing subsequence in an array.

Acceptance 100%
Also Available On
Other platform versions and source mappings for the same problem.

LIS

gfg
Primary

Increasing Subsequence

gfg
Problem Statement

Given an integer array nums, determine the length of the longest subsequence whose values are strictly increasing.

A subsequence is formed by deleting zero or more elements without changing the order of the remaining elements. The elements do not need to be contiguous.

Your task is to return only the maximum length of such a subsequence.

Input Format

  • An integer array nums.
  • The array may contain positive, negative, or repeated values.

Output Format

  • Return a single integer: the length of the longest strictly increasing subsequence in nums.

Constraints

  • 1 <= nums.length <= 2500
  • $-10^{4}$ <= nums[i] <= $10^{4}$
  • The answer is at least 1 when the array is non-empty.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [10,9,2,5,3,7,101,18]

Output

4

Explanation

One longest increasing subsequence is [2,3,7,101], which has length 4.

Example 2

Input

nums = [0,1,0,3,2,3]

Output

4

Explanation

One longest increasing subsequence is [0,1,2,3].

Show 1 more example

Example 3

Input

nums = [7,7,7,7,7]

Output

1

Explanation

Any single element is an increasing subsequence, but no longer strictly increasing subsequence exists.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.