|
| 1 | +<h2><a href="https://leetcode.com/problems/count-partitions-with-max-min-difference-at-most-k">3835. Count Partitions With Max-Min Difference at Most K</a></h2><h3>Medium</h3><hr><p>You are given an integer array <code>nums</code> and an integer <code>k</code>. Your task is to partition <code>nums</code> into one or more <strong>non-empty</strong> contiguous segments such that in each segment, the difference between its <strong>maximum</strong> and <strong>minimum</strong> elements is <strong>at most</strong> <code>k</code>.</p> |
| 2 | + |
| 3 | +<p>Return the total number of ways to partition <code>nums</code> under this condition.</p> |
| 4 | + |
| 5 | +<p>Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | + |
| 10 | +<div class="example-block"> |
| 11 | +<p><strong>Input:</strong> <span class="example-io">nums = [9,4,1,3,7], k = 4</span></p> |
| 12 | + |
| 13 | +<p><strong>Output:</strong> <span class="example-io">6</span></p> |
| 14 | + |
| 15 | +<p><strong>Explanation:</strong></p> |
| 16 | + |
| 17 | +<p>There are 6 valid partitions where the difference between the maximum and minimum elements in each segment is at most <code>k = 4</code>:</p> |
| 18 | + |
| 19 | +<ul> |
| 20 | + <li><code>[[9], [4], [1], [3], [7]]</code></li> |
| 21 | + <li><code>[[9], [4], [1], [3, 7]]</code></li> |
| 22 | + <li><code>[[9], [4], [1, 3], [7]]</code></li> |
| 23 | + <li><code>[[9], [4, 1], [3], [7]]</code></li> |
| 24 | + <li><code>[[9], [4, 1], [3, 7]]</code></li> |
| 25 | + <li><code>[[9], [4, 1, 3], [7]]</code></li> |
| 26 | +</ul> |
| 27 | +</div> |
| 28 | + |
| 29 | +<p><strong class="example">Example 2:</strong></p> |
| 30 | + |
| 31 | +<div class="example-block"> |
| 32 | +<p><strong>Input:</strong> <span class="example-io">nums = [3,3,4], k = 0</span></p> |
| 33 | + |
| 34 | +<p><strong>Output:</strong> <span class="example-io">2</span></p> |
| 35 | + |
| 36 | +<p><strong>Explanation:</strong></p> |
| 37 | + |
| 38 | +<p>There are 2 valid partitions that satisfy the given conditions:</p> |
| 39 | + |
| 40 | +<ul> |
| 41 | + <li><code>[[3], [3], [4]]</code></li> |
| 42 | + <li><code>[[3, 3], [4]]</code></li> |
| 43 | +</ul> |
| 44 | +</div> |
| 45 | + |
| 46 | +<p> </p> |
| 47 | +<p><strong>Constraints:</strong></p> |
| 48 | + |
| 49 | +<ul> |
| 50 | + <li><code>2 <= nums.length <= 5 * 10<sup>4</sup></code></li> |
| 51 | + <li><code>1 <= nums[i] <= 10<sup>9</sup></code></li> |
| 52 | + <li><code>0 <= k <= 10<sup>9</sup></code></li> |
| 53 | +</ul> |
0 commit comments