oneby7

1-D Dynamic Programming Increasing subsequences

528. Number of Longest Increasing Subsequence

Medium

You are given an array of integers nums. Find the length of its longest strictly increasing subsequence, and return how many strictly increasing subsequences have that length.

A subsequence keeps some of the elements, in their original order, and drops the rest. It is strictly increasing when every kept element is bigger than the one kept before it, so two equal values can never both be in it.

Two subsequences are different when they keep different positions, even if their values are the same.

Example 1

Input: nums = [1, 4, 2, 5, 3, 6]

Output: 3

Explanation: The longest increasing subsequences have length 4. There are three of them: 1, 4, 5, 6, then 1, 2, 5, 6, then 1, 2, 3, 6.

Example 2

Input: nums = [5, 5, 5]

Output: 3

Explanation: Equal values cannot both be kept, so the longest length is 1. Each of the three elements alone is one such subsequence.

Example 3

Input: nums = [-7, 10, -7, 0, 10]

Output: 2

Explanation: The longest length is 3, from -7, 0, 10. Either -7 can start it, and they sit at different positions, so they make two subsequences.

Check your understanding

Work out the answer for this input before you write any code.

nums = [1, 3, 2, 2, 4]

Constraints

  • 1 ≤ nums.length ≤ 105
  • -109 ≤ nums[i] ≤ 109
  • The answer is less than 231.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.