Skip to contentKEEP THE CRAFTTHE SANCTUARY

Count Subsequences by Minimum and Maximum

Count the subsequences whose smallest plus largest value is at most target.

The problem

Implement Solution.numSubseq(nums, target). Count the non-empty subsequences of nums whose minimum value plus maximum value is at most target. Subsequences are picked by position, so equal values at different positions give different subsequences. Return the count modulo 1,000,000,007. For example, with nums = [3, 5, 6, 7] and target = 9 the answer is 4: [3], [3, 5], [3, 5, 6] and [3, 6]. nums has between 1 and 100,000 values, each from 1 to 1,000,000, and 1 <= target <= 1,000,000.

Starter code

class Solution:
    def numSubseq(self, nums, target):
        pass

Public examples

  1. nums = [3, 5, 6, 7], target = 9gives4
  2. nums = [5], target = 9gives0
  3. nums = [2, 2, 2], target = 4gives7

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.