2948. Make Lexicographically Smallest Array by Swapping Elements
</> Solution
/**
* Problem: LeetCode 2948 - Make Lexicographically Smallest Array by Swapping Elements
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
import java.util.*;
/**
* Problem: LeetCode 2948 - Make Lexicographically Smallest Array by Swapping Elements
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int[] lexicographicallySmallestArray(int[] nums, int limit) {
int n = nums.length;
// Step 1: index array banao aur value ke basis par sort karo
Integer[] idx = new Integer[n];
for (int i = 0; i < n; i++) idx[i] = i;
Arrays.sort(idx, (a, b) -> nums[a] - nums[b]);
int[] result = new int[n];
// Step 2: Groups banao - consecutive sorted elements jinka diff <= limit
int i = 0;
while (i < n) {
int j = i + 1;
// Group extend karo jab tak consecutive sorted values ka diff <= limit
while (j < n && nums[idx[j]] - nums[idx[j-1]] <= limit) {
j++;
}
// Group: idx[i..j-1]
// In indices ko sort karo (original positions)
// Aur sorted values assign karo in sorted positions par
// Group ke original indices nikalo
List<Integer> groupIndices = new ArrayList<>();
for (int k = i; k < j; k++) {
groupIndices.add(idx[k]);
}
// Positions sort karo
Collections.sort(groupIndices);
// Sorted positions par sorted values assign karo
for (int k = 0; k < groupIndices.size(); k++) {
result[groupIndices.get(k)] = nums[idx[i + k]];
}
i = j;
}
return result;
}
}
"""
Problem: LeetCode 2948 - Make Lexicographically Smallest Array by Swapping Elements
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
class Solution:
def lexicographicallySmallestArray(self, nums: list[int], limit: int) -> list[int]:
n = len(nums)
# Sort indices based on their values in nums
idx = sorted(range(n), key=lambda x: nums[x])
result = [0] * n
i = 0
while i < n:
j = i + 1
# Extend group as long as adjacent sorted values differ by <= limit
while j < n and nums[idx[j]] - nums[idx[j - 1]] <= limit:
j += 1
# Extract and sort original indices within the connected component
group_indices = sorted(idx[i:j])
# Assign sorted values to sorted positions
for k in range(len(group_indices)):
result[group_indices[k]] = nums[idx[i + k]]
i = j
return result
/**
* Problem: LeetCode 2948 - Make Lexicographically Smallest Array by Swapping Elements
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
/**
* Problem: LeetCode 2948 - Make Lexicographically Smallest Array by Swapping Elements
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {
int n = nums.size();
vector<int> idx(n);
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(), [&](int a, int b) {
return nums[a] < nums[b];
});
vector<int> result(n);
int i = 0;
while (i < n) {
int j = i + 1;
while (j < n && nums[idx[j]] - nums[idx[j - 1]] <= limit) {
j++;
}
// Extract and sort indices within this connected component
vector<int> groupIndices(idx.begin() + i, idx.begin() + j);
sort(groupIndices.begin(), groupIndices.end());
// Assign sorted values to sorted index positions
for (int k = 0; k < (int)groupIndices.size(); k++) {
result[groupIndices[k]] = nums[idx[i + k]];
}
i = j;
}
return result;
}
};
/**
* Problem: LeetCode 2948 - Make Lexicographically Smallest Array by Swapping Elements
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var lexicographicallySmallestArray = function(nums, limit) {
const n = nums.length;
const idx = new Int32Array(n);
for (let i = 0; i < n; i++) idx[i] = i;
idx.sort((a, b) => nums[a] - nums[b]);
const result = new Int32Array(n);
let i = 0;
while (i < n) {
let j = i + 1;
while (j < n && nums[idx[j]] - nums[idx[j - 1]] <= limit) {
j++;
}
// Extract and sort indices within this connected component
const groupIndices = Array.from(idx.subarray(i, j)).sort((a, b) => a - b);
for (let k = 0; k < groupIndices.length; k++) {
result[groupIndices[k]] = nums[idx[i + k]];
}
i = j;
}
return Array.from(result);
};