150 XP

32. Longest increasing subsequence

Expert dynamic programming lists

A subsequence is what you get by picking some items from a list and keeping them in their original order. You can skip items, so the ones you pick don't have to be next to each other: [2, 7, 18] is a subsequence of [2, 5, 7, 18], but [7, 2] is not.

A subsequence is strictly increasing when each number is bigger than the one before it. An equal number doesn't count: [2, 2] is not increasing.

Write a function lis_length(numbers) that takes a list of whole numbers and returns the length of its longest strictly increasing subsequence, as a whole number. You return only how long it is, not the subsequence itself. If the list is empty, return 0.

Example 1

Inputnumbers = [10, 9, 2, 5, 3, 7, 101, 18]
Output4
Explanation[2, 3, 7, 101] is one; [2, 5, 7, 18] too.

Example 2

Inputnumbers = [3, 2, 1]
Output1
ExplanationEach is smaller than the last: pick one.

Example 3

Inputnumbers = [1, 3, 3, 4]
Output3
Explanation[1, 3, 4]: the two 3s can't both be used.

Constraints

  • numbers has 0 to 100 items.
  • Each item is a whole number from -1000 to 1000. Numbers can repeat.

Submit also runs 5 more tests you can't see.

Code
def lis_length(numbers):
    """Length of the longest strictly increasing subsequence.

    numbers: list of int; may be empty, may repeat
    Returns: int, how many numbers the longest strictly
        increasing subsequence has (0 for an empty list)
    """
    # Write your code here
    pass