#3578

Count Partitions With Max-Min Difference at Most K

medium · 58.8% accepted · 528 likes · top 55%

array · dynamic programming · queue · sliding window · prefix sum · monotonic queue

⊣ practice⊣ open on leetcode ↗

Description

You are given an integer array nums and an integer k. Your task is to partition nums into one or more non-empty contiguous segments such that in each segment, the difference between its maximum and minimum elements is at most k.

Return the total number of ways to partition nums under this condition.

Since the answer may be too large, return it modulo 109 + 7.

Solution