Understanding Longest Increasing Subsequence
Return the length of the longest strictly increasing subsequence of nums. Selected values preserve their order but need not be contiguous.
How it works
Store the best subsequence ending at each index and extend only smaller earlier values.
Initialize each nonempty ending state to one. Try every earlier index with a smaller value. Return the largest ending state, or zero for empty input.
Try an example
Input: nums = [10, 9, 2, 5, 3, 7, 101, 18]
Output: 4
Store the best subsequence ending at each index and extend only smaller earlier values.
Constraints
- 0–32 integers between -1,000,000 and 1,000,000.
- Equal values do not extend a strictly increasing subsequence.
Use the simulator
Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.
Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.