Skip to content

Commit 8c36701

Browse files
committed
Sync LeetCode submission Runtime - 73 ms (90.30%), Memory - 33.1 MB (76.79%)
1 parent 2c3fe09 commit 8c36701

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is a multiple of 3 and a positive integer <code>k</code>.</p>
2+
3+
<p>Divide the array <code>nums</code> into <code>n / 3</code> arrays of size <strong>3</strong> satisfying the following condition:</p>
4+
5+
<ul>
6+
<li>The difference between <strong>any</strong> two elements in one array is <strong>less than or equal</strong> to <code>k</code>.</li>
7+
</ul>
8+
9+
<p>Return a <strong>2D</strong> array containing the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return <strong>any</strong> of them.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<div class="example-block">
15+
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,4,8,7,9,3,5,1], k = 2</span></p>
16+
17+
<p><strong>Output:</strong> <span class="example-io">[[1,1,3],[3,4,5],[7,8,9]]</span></p>
18+
19+
<p><strong>Explanation:</strong></p>
20+
21+
<p>The difference between any two elements in each array is less than or equal to 2.</p>
22+
</div>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<div class="example-block">
27+
<p><strong>Input:</strong> <span class="example-io">nums = [2,4,2,2,5,2], k = 2</span></p>
28+
29+
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
30+
31+
<p><strong>Explanation:</strong></p>
32+
33+
<p>Different ways to divide <code>nums</code> into 2 arrays of size 3 are:</p>
34+
35+
<ul>
36+
<li>[[2,2,2],[2,4,5]] (and its permutations)</li>
37+
<li>[[2,2,4],[2,2,5]] (and its permutations)</li>
38+
</ul>
39+
40+
<p>Because there are four 2s there will be an array with the elements 2 and 5 no matter how we divide it. since <code>5 - 2 = 3 &gt; k</code>, the condition is not satisfied and so there is no valid division.</p>
41+
</div>
42+
43+
<p><strong class="example">Example 3:</strong></p>
44+
45+
<div class="example-block">
46+
<p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,8,2,12,7,12,10,5,8,5,5,7,9,2,5,11], k = 14</span></p>
47+
48+
<p><strong>Output:</strong> <span class="example-io">[[2,2,12],[4,8,5],[5,9,7],[7,8,5],[5,9,10],[11,12,2]]</span></p>
49+
50+
<p><strong>Explanation:</strong></p>
51+
52+
<p>The difference between any two elements in each array is less than or equal to 14.</p>
53+
</div>
54+
55+
<p>&nbsp;</p>
56+
<p><strong>Constraints:</strong></p>
57+
58+
<ul>
59+
<li><code>n == nums.length</code></li>
60+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
61+
<li><code>n </code>is a multiple of 3</li>
62+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
63+
<li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li>
64+
</ul>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Approach: Sorting
2+
3+
# Time: O(n log n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def divideArray(self, nums: List[int], k: int) -> List[List[int]]:
8+
nums.sort()
9+
ans = []
10+
11+
for i in range(0, len(nums), 3):
12+
if nums[i + 2] - nums[i] > k:
13+
return []
14+
ans.append(nums[i : i + 3])
15+
16+
return ans
17+

0 commit comments

Comments
 (0)