2948. Make Lexicographically Smallest Array by Swapping Elements #1222
-
Topics: You are given a 0-indexed array of positive integers In one operation, you can choose any two indices Return the lexicographically smallest array that can be obtained by performing the operation any number of times. An array Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The problem asks us to find the lexicographically smallest array by swapping elements of an array, subject to a condition. Specifically, we can only swap two elements Key Points
Approach
Plan
Let's implement this solution in PHP: 2948. Make Lexicographically Smallest Array by Swapping Elements <?php
/**
* @param Integer[] $nums
* @param Integer $limit
* @return Integer[]
*/
function lexicographicallySmallestArray($nums, $limit) {
$n = count($nums);
$ans = array_fill(0, $n, 0);
// Get pairs of (num, index), sorted by num
$numAndIndexes = $this->getNumAndIndexes($nums);
// Groups of [(num, index)] where the difference between numbers <= limit
$numAndIndexesGroups = [];
foreach ($numAndIndexes as $numAndIndex) {
if (empty($numAndIndexesGroups) ||
$numAndIndex[0] - end($numAndIndexesGroups[count($numAndIndexesGroups) - 1])[0] > $limit) {
// Start a new group
$numAndIndexesGroups[] = [$numAndIndex];
} else {
// Append to the existing group
$numAndIndexesGroups[count($numAndIndexesGroups) - 1][] = $numAndIndex;
}
}
// Sort each group and place the values in the correct positions
foreach ($numAndIndexesGroups as $numAndIndexesGroup) {
$sortedNums = [];
$sortedIndices = [];
foreach ($numAndIndexesGroup as $pair) {
$sortedNums[] = $pair[0];
$sortedIndices[] = $pair[1];
}
// Sort the indices and the values independently
sort($sortedNums);
sort($sortedIndices);
// Assign sorted values to the respective indices in the answer
for ($i = 0; $i < count($sortedNums); $i++) {
$ans[$sortedIndices[$i]] = $sortedNums[$i];
}
}
return $ans;
}
/**
* @param $nums
* @return array
*/
function getNumAndIndexes($nums) {
$numAndIndexes = [];
foreach ($nums as $index => $num) {
$numAndIndexes[] = [$num, $index];
}
usort($numAndIndexes, function($a, $b) {
return $a[0] <=> $b[0];
});
return $numAndIndexes;
}
// Example usage:
$nums1 = [1, 5, 3, 9, 8];
$limit1 = 2;
print_r(lexicographicallySmallestArray($nums1, $limit1)); // Output: [1, 3, 5, 8, 9]
$nums2 = [1, 7, 6, 18, 2, 1];
$limit2 = 3;
print_r(lexicographicallySmallestArray($nums2, $limit2)); // Output: [1, 6, 7, 18, 1, 2]
$nums3 = [1, 7, 28, 19, 10];
$limit3 = 3;
print_r(lexicographicallySmallestArray($nums3, $limit3)); // Output: [1, 7, 28, 19, 10]
$nums4 = [1, 60, 34, 84, 62, 56, 39, 76, 49, 38];
$limit4 = 4;
print_r(lexicographicallySmallestArray($nums4, $limit4)); // Output: [1, 56, 34, 84, 60, 62, 38, 76, 49, 39]
?> Explanation:
Example WalkthroughExample 1Input:
Time Complexity
Overall Time Complexity: O(n log n) Output for ExamplesExample 2Input: Example 3Input: This approach efficiently handles the problem by using sorting to identify connected components and rearranging values within each component to achieve the lexicographically smallest array. By leveraging sorting and group processing, we ensure an optimal solution with O(n log n) complexity. |
Beta Was this translation helpful? Give feedback.
The problem asks us to find the lexicographically smallest array by swapping elements of an array, subject to a condition. Specifically, we can only swap two elements
nums[i]
andnums[j]
if the absolute difference between them (|nums[i] - nums[j]|
) is less than or equal to a givenlimit
.Key Points
a
is lexicographically smaller thanb
if at the first differing index,a[i] < b[i]
.limit
.