Chaturmind
LearnDSASystem DesignInterview PrepDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth
  • Java Interview Prep

Company

  • About
  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

👥10,172visitors

Built for engineers who want to go deep.

← Dynamic Programming Patterns

DP Fundamentals

  • Introduction to Dynamic Programming
  • 1D DP — Climbing Stairs to House Robber
  • Practice problems

    Coin Change
  • Longest Increasing Subsequence
  • Word Break
  • House Robber
  • Climbing Stairs

String & Subsequence DP

  • Longest Common Subsequence
  • 0/1 Knapsack & Subsets

Advanced DP

  • Interval DP
  • State Machine DP
  • Digit DP
Chaturmind
← Dynamic Programming Patterns

DP Fundamentals

  • Introduction to Dynamic Programming
  • 1D DP — Climbing Stairs to House Robber
  • Practice problems

    Coin Change
  • Longest Increasing Subsequence
  • Word Break
  • House Robber
  • Climbing Stairs

String & Subsequence DP

  • Longest Common Subsequence
  • 0/1 Knapsack & Subsets

Advanced DP

  • Interval DP
  • State Machine DP
  • Digit DP
HomeLearnDynamic Programming PatternsDP Fundamentals
MediumDynamic Programming

Longest Increasing Subsequence

dynamic-programmingbinary-search

Problem

Given an integer array nums, return the length of the longest strictly increasing subsequence.

A subsequence keeps the original order, but may skip elements. It doesn't have to be contiguous.

Input:  nums = [10, 9, 2, 5, 3, 7, 101, 18]
Output: 4          (for example 2, 3, 7, 101 — or 2, 5, 7, 18)
  • "Strictly increasing" means equal values can't both be used: [7, 7, 7] has an answer of 1.
  • An O(n²) dynamic program is the natural first solution.
  • Follow-up: can you reach O(n log n)?

Examples

Example 1

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

Output: 4

Explanation: [2,3,7,101]

Example 2

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

Output: 4

Explanation: [0,1,2,3]

Constraints

  • •1 <= nums.length <= 2500
  • •-10^4 <= nums[i] <= 10^4

Hints

Hint 1

The direct O(n^2) DP: dp[i] = length of the longest increasing subsequence ENDING at index i, built by checking every earlier index j.

Hint 2

dp[i] = 1 + max(dp[j]) for every j < i where nums[j] < nums[i] — or just 1 if no such j exists.

Hint 3

The O(n log n) improvement replaces the O(n) inner scan with binary search, by maintaining a different kind of state: not 'best ending here,' but 'smallest possible tail for each achievable length.'

Solutions

public int lengthOfLISDP(int[] nums) {
    int n = nums.length;
    int[] dp = new int[n];
    Arrays.fill(dp, 1); // every element is a subsequence of length 1 on its own
    int maxLen = 1;
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            if (nums[j] < nums[i]) {
                dp[i] = Math.max(dp[i], dp[j] + 1);
            }
        }
        maxLen = Math.max(maxLen, dp[i]);
    }
    return maxLen;
}

Time: O(n^2) · Space: O(n)

Advertisement

Previous · Practice problem

Coin Change

Next · Practice problem

Word Break