2948. Make Lexicographically Smallest Array by Swapping Elements
Difficulty: Medium
Topics: Array, Union Find, Sorting
You are given a 0-indexed array of positive integers nums and a positive integer limit.
In one operation, you can choose any two indices i and j and swap nums[i] and nums[j] if |nums[i] - nums[j]| <= limit.
Return the lexicographically smallest array that can be obtained by performing the operation any number of times.
An array a is lexicographically smaller than an array b if in the first position where a and b differ, array a has an element that is less than the corresponding element in b. For example, the array [2,10,3] is lexicographically smaller than the array [10,2,3] because they differ at index 0 and 2 < 10.
Example 1:
Example 2:
Example 3:
Example 4:
Constraints:
Hint:
Solution:
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] and nums[j] if the absolute difference between them (|nums[i] - nums[j]|) is less than or equal to a given limit.
Let's implement this solution in PHP: 2948. Make Lexicographically Smallest Array by Swapping Elements
Explanation:
Extracting and Sorting (getNumAndIndexes):
- Combine values and indices into pairs for easy reference.
- Sort the pairs by value to enable efficient grouping of connected components.
Grouping Logic:
- Traverse the sorted pairs. If the difference between consecutive values is ≤ limit, add them to the same group; otherwise, start a new group.
Sorting and Reassigning:
- For each group:
- Extract the indices and values.
- Sort both lists to ensure the smallest values are placed in the smallest indices.
- Reassign the sorted values to their respective positions in the answer array.
Result Construction:
- After processing all groups, return the updated array.
Example Walkthrough
Example 1
Input: nums = [1,5,3,9,8], limit = 2
Extract and Sort:
- Pairs: [(1, 0), (5, 1), (3, 2), (9, 3), (8, 4)]
- Sorted Pairs: [(1, 0), (3, 2), (5, 1), (8, 4), (9, 3)]
Grouping:
- Group 1: [(1, 0)]
- Group 2: [(3, 2), (5, 1)]
- Group 3: [(8, 4), (9, 3)]
Sorting Groups:
- Group 1: No change ([1])
- Group 2: Values = [3, 5], Indices = [1, 2] → Result: [1, 3, 5]
- Group 3: Values = [8, 9], Indices = [3, 4] → Result: [8, 9]
Final Result: [1, 3, 5, 8, 9]
Time Complexity
- Sorting: Sorting the nums array takes O(n log n).
- Grouping: Linear traversal through the sorted array takes O(n).
- Sorting Groups: Sorting indices and values for each group takes O(k log k), where k is the group size. Summed over all groups, this is O(n log n).
Overall Time Complexity: O(n log n)
Output for Examples
Example 2
Input: nums = [1,7,6,18,2,1], limit = 3
Output: [1,6,7,18,1,2]Example 3
Input: nums = [1,7,28,19,10], limit = 3
Output: [1,7,28,19,10]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.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks ?. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
The above is the detailed content of Make Lexicographically Smallest Array by Swapping Elements. For more information, please follow other related articles on the PHP Chinese website!