1. Empty input
No numbers means no subsequence.
if (nums.length === 0) return 0Typorithm · medium
Step 1 of 8 · intro card
Dynamic Programming
LIS DP stores the best increasing subsequence length ending at each index.
The state is anchored at an ending index.
Look back at earlier smaller values.
Extend the best compatible subsequence.
State: dp[i] is LIS ending at i.
Base: every element alone has length 1.
Transition: max(dp[j] + 1).
Answer: max over all endings.
function lengthOfLIS(nums) {
if (nums.length === 0) return 0;
const dp = Array(nums.length).fill(1);
for (let i = 1; i < nums.length; i++) {
for (let j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
return Math.max(...dp);
}Connect each part of the code to the concept before you type.
No numbers means no subsequence.
if (nums.length === 0) return 0Every number alone forms an increasing subsequence of length 1.
Array(nums.length).fill(1)Only smaller previous values can come before nums[i].
if (nums[j] < nums[i])Extend the best subsequence ending at j.
dp[i] = Math.max(dp[i], dp[j] + 1)Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
InputSequence 3,1,2,5,4
Follow temporary values, local state and supporting structures at this step.
↗ The 5-cell working table above is itself auxiliary memory. Input values are shown separately.
Input · not auxiliary memory
Sequence5 entries
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state