Find Number of Good Subsequences π
Learn this problemProblem statement
A positive integer array is removable if it can be reduced to one element by performing the following operation any number of times, possibly zero:
- Choose two adjacent values
iandjsuch thati > j. - Remove
jand add its value toi.
Here i and j are values, not indices.
An array is good if every contiguous subarray of it is removable.
A proper subsequence selects at least one element while preserving order, but does not select the entire original array. Return the number of non-empty proper subsequences of A that are good, modulo 10^9 + 7.
Function
findNumberOfGoodSubsequences(A: int[]) β intExamples
Example 1
A = [2, 4, 2, 2]return = 9For positive values, a selected sequence is good exactly when no two adjacent selected values are equal. There are 9 non-empty proper subsequences of [2, 4, 2, 2] with that property.
Example 2
A = [1, 2, 3]return = 6All 7 non-empty subsequences have unequal adjacent selected values. The complete subsequence [1, 2, 3] is excluded because only proper subsequences are counted, leaving 6.
Constraints
1 <= A.length- Every value in
Ais a positive integer.