Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Count Partitions With Max-Min Difference at Most K

XPChallenge Points: 20
levelLevel: Medium

You are given an integer array nums and an integer k.
Your goal is to divide the array into one or more continuous non-empty segments such that, for every segment, the difference between the largest and smallest elements is at most k.

Return the total number of possible partitions that satisfy this property.

Because the result could be very large, return the answer modulo (10⁹ + 7).

Example 1:

Input: nums = [2, 5, 4], k = 3

Output: 4

Explanation:

Possible valid partitions: [[2], [5], [4]] [[2,5], [4]] [[2], [5,4]] Each segment’s (max - min) ≤ 3, so total = 3.

Example 2:

Input: nums = [1, 2, 3, 6], k = 2

Output: 4

Explanation:

Valid partitions include: [[1], [2], [3], [6]] [[1,2,3], [6]] [[1,2], [3], [6]] [[1], [2,3], [6]] No other configuration keeps all (max - min) ≤ 2.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result