Skip to content

Commit faa45c0

Browse files
committed
Sync LeetCode submission Runtime - 280 ms (83.33%), Memory - 96.2 MB (5.56%)
1 parent 0173b76 commit faa45c0

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p>
2+
3+
<p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <strong>if</strong> <code>|nums[i] - nums[j]| &lt;= limit</code>.</p>
4+
5+
<p>Return <em>the <strong>lexicographically smallest array</strong> that can be obtained by performing the operation any number of times</em>.</p>
6+
7+
<p>An array <code>a</code> is lexicographically smaller than an array <code>b</code> if in the first position where <code>a</code> and <code>b</code> differ, array <code>a</code> has an element that is less than the corresponding element in <code>b</code>. For example, the array <code>[2,10,3]</code> is lexicographically smaller than the array <code>[10,2,3]</code> because they differ at index <code>0</code> and <code>2 &lt; 10</code>.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre>
13+
<strong>Input:</strong> nums = [1,5,3,9,8], limit = 2
14+
<strong>Output:</strong> [1,3,5,8,9]
15+
<strong>Explanation:</strong> Apply the operation 2 times:
16+
- Swap nums[1] with nums[2]. The array becomes [1,3,5,9,8]
17+
- Swap nums[3] with nums[4]. The array becomes [1,3,5,8,9]
18+
We cannot obtain a lexicographically smaller array by applying any more operations.
19+
Note that it may be possible to get the same result by doing different operations.
20+
</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> nums = [1,7,6,18,2,1], limit = 3
26+
<strong>Output:</strong> [1,6,7,18,1,2]
27+
<strong>Explanation:</strong> Apply the operation 3 times:
28+
- Swap nums[1] with nums[2]. The array becomes [1,6,7,18,2,1]
29+
- Swap nums[0] with nums[4]. The array becomes [2,6,7,18,1,1]
30+
- Swap nums[0] with nums[5]. The array becomes [1,6,7,18,1,2]
31+
We cannot obtain a lexicographically smaller array by applying any more operations.
32+
</pre>
33+
34+
<p><strong class="example">Example 3:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> nums = [1,7,28,19,10], limit = 3
38+
<strong>Output:</strong> [1,7,28,19,10]
39+
<strong>Explanation:</strong> [1,7,28,19,10] is the lexicographically smallest array we can obtain because we cannot apply the operation on any two indices.
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
47+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
48+
<li><code>1 &lt;= limit &lt;= 10<sup>9</sup></code></li>
49+
</ul>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Approach: Sorting + Grouping
2+
3+
# Time: O(n * log n)
4+
# Space: O(n)
5+
6+
from collections import deque
7+
8+
class Solution:
9+
def lexicographicallySmallestArray(self, nums: List[int], limit: int) -> List[int]:
10+
nums_sorted = sorted(nums)
11+
12+
curr_group = 0
13+
num_to_group = {}
14+
num_to_group[nums_sorted[0]] = curr_group
15+
16+
group_to_list = {}
17+
group_to_list[curr_group] = deque([nums_sorted[0]])
18+
19+
for i in range(1, len(nums)):
20+
if abs(nums_sorted[i] - nums_sorted[i - 1]) > limit:
21+
# new group
22+
curr_group += 1
23+
24+
num_to_group[nums_sorted[i]] = curr_group
25+
26+
if curr_group not in group_to_list:
27+
group_to_list[curr_group] = deque()
28+
group_to_list[curr_group].append(nums_sorted[i])
29+
30+
# Iterate through the input and overwrite each element with the next element in its corresponding group
31+
for i in range(len(nums)):
32+
num = nums[i]
33+
group = num_to_group[num]
34+
nums[i] = group_to_list[group].popleft()
35+
36+
return nums

0 commit comments

Comments
 (0)