Skip to content

Commit a15d519

Browse files
committed
Sync LeetCode submission Runtime - 75 ms (92.99%), Memory - 29.1 MB (73.61%)
1 parent 10bad32 commit a15d519

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>. You may partition <code>nums</code> into one or more <strong>subsequences</strong> such that each element in <code>nums</code> appears in <strong>exactly</strong> one of the subsequences.</p>
2+
3+
<p>Return <em>the <strong>minimum </strong>number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is <strong>at most</strong> </em><code>k</code><em>.</em></p>
4+
5+
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> nums = [3,6,1,2,5], k = 2
12+
<strong>Output:</strong> 2
13+
<strong>Explanation:</strong>
14+
We can partition nums into the two subsequences [3,1,2] and [6,5].
15+
The difference between the maximum and minimum value in the first subsequence is 3 - 1 = 2.
16+
The difference between the maximum and minimum value in the second subsequence is 6 - 5 = 1.
17+
Since two subsequences were created, we return 2. It can be shown that 2 is the minimum number of subsequences needed.
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> nums = [1,2,3], k = 1
24+
<strong>Output:</strong> 2
25+
<strong>Explanation:</strong>
26+
We can partition nums into the two subsequences [1,2] and [3].
27+
The difference between the maximum and minimum value in the first subsequence is 2 - 1 = 1.
28+
The difference between the maximum and minimum value in the second subsequence is 3 - 3 = 0.
29+
Since two subsequences were created, we return 2. Note that another optimal solution is to partition nums into the two subsequences [1] and [2,3].
30+
</pre>
31+
32+
<p><strong class="example">Example 3:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> nums = [2,2,4,5], k = 0
36+
<strong>Output:</strong> 3
37+
<strong>Explanation:</strong>
38+
We can partition nums into the three subsequences [2,2], [4], and [5].
39+
The difference between the maximum and minimum value in the first subsequences is 2 - 2 = 0.
40+
The difference between the maximum and minimum value in the second subsequences is 4 - 4 = 0.
41+
The difference between the maximum and minimum value in the third subsequences is 5 - 5 = 0.
42+
Since three subsequences were created, we return 3. It can be shown that 3 is the minimum number of subsequences needed.
43+
</pre>
44+
45+
<p>&nbsp;</p>
46+
<p><strong>Constraints:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
50+
<li><code>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
51+
<li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li>
52+
</ul>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Approach: Sort + Greedy
2+
3+
# Time: O(n log n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def partitionArray(self, nums: List[int], k: int) -> int:
8+
nums.sort()
9+
ans = 1
10+
rec = nums[0]
11+
12+
for num in nums:
13+
if num - rec > k:
14+
ans += 1
15+
rec = num
16+
17+
return ans
18+

0 commit comments

Comments
 (0)