2Brute2Best
🔥0

Number of Longest Increasing Subsequence

Medium·LeetCode #673 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given a list of whole numbers nums.

Count the longest strictly increasing subsequences. Two subsequences are different when they pick different positions, even if their values match.

Input

An integer array nums.

Output

One number: how many longest strictly increasing subsequences there are.

Example 1
Input: nums = [1, 3, 5, 4, 7]
Output: 2

The longest length is 4. Two chains reach it: 1, 3, 4, 7 and 1, 3, 5, 7.

Example 2
Input: nums = [2, 2, 2, 2, 2]
Output: 5

The longest length is 1. Each single 2 is one chain, so there are five chains.

Constraints
  • • 1 ≤ nums.length ≤ 2000
  • • −10^6 ≤ nums[i] ≤ 10^6
  • • The answer is guaranteed to fit in a 32-bit signed integer.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“number of the longest”
→ We count chains, not just find the length. Two subsequences that pick different positions count as different, even with equal values.
“strictly increasing”
→ Equal values cannot both be in the chain, so the check is <, not <=.
“subsequences”
→ Chains can skip numbers, so each end position starts its own count of chains.
03

Approach Complexity Ladder

Brute Force — list every subset of positionsO(N · 2^N)O(1)

Try all 2^N subsets of positions; keep the increasing ones and count the longest

Optimal — len and cnt for each endO(N^2)O(N)

For each end, keep its longest length and how many chains reach it; O(N²) time

Brute Force Intuition

Each subset of positions is one candidate subsequence. Check each subset: if its values are strictly increasing, record its length. Then count the subsets that have the biggest length.

Recognition rule for this sub-pattern:

O(n²): try every earlier j. O(n log n): binary search the tails array.
Remember this · DP — Longest Increasing Subsequence
Trigger
Longest chain where each item must be bigger/fit inside the previous one (numbers, pairs, envelopes, words).
Think
Does each element extend the best chain ending at some smaller earlier element?
Key question
Sort first? Then what does "increasing" mean for these items?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like DP — Longest Increasing Subsequence translate into production architectures and low-level memory mechanics.