40. Longest increasing subsequence
A subsequence keeps some elements of a list in their original order, not necessarily next to each other. Write lengthOfLIS nums, the length of the longest subsequence of nums that is strictly increasing.
Examples
- Input
lengthOfLIS [10, 9, 2, 5, 3, 7, 101, 18]Output4 - Input
lengthOfLIS [0, 1, 0, 3, 2, 3]Output4 - Input
lengthOfLIS [7, 7, 7, 7]Output1
Submitting also runs 9 hidden tests.
Constraints
- Hidden tests include 2,000 numbers. O(n²) and faster methods pass; trying every subsequence does not.
Running and submitting solutions, the hints and the editorial come with the Academy Pass. The daily problem is open to every account.
Loading editor…
Type
\to in the editor, or click:Run checks your code against the examples and your custom inputs and records nothing. Submit also runs the hidden tests, confirms the axioms your proof uses and records the result. Each uses one compiler check.
ReadyLn 1, Col 1Lean 4
Draft saved in this browser