2948. Make Lexicographically Smallest Array by Swapping Elements
UnknownView on LeetCode
Time: O(n log n)
Space: O(n)
Problem Overview
Make Lexicographically Smallest Array by Swapping Elements (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Greedy pattern in coding interviews. Sort with original indices; group by proximity (diff ≤ limit); assign sorted values to slots.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Sort with original indices; group by proximity (diff ≤ limit); assign sorted values to slots.
2948.cs
C#
// Approach: Sort with original indices; group by proximity (diff ≤ limit); assign sorted values to slots.
// Time: O(n log n) Space: O(n)
public class Solution
{
public int[] LexicographicallySmallestArray(int[] nums, int limit)
{
int[] ans = new int[nums.Length];
List<List<KeyValuePair<int, int>>> numAndIndexesGroups = new List<List<KeyValuePair<int, int>>>();
foreach (var numAndIndex in GetNumAndIndexes(nums))
{
if (numAndIndexesGroups.Count == 0 ||
numAndIndex.Key - numAndIndexesGroups.Last().Last().Key > limit)
// Start a new group.
numAndIndexesGroups.Add(new List<KeyValuePair<int, int>> { numAndIndex });
else
// Append to the existing group.
numAndIndexesGroups.Last().Add(numAndIndex);
}
foreach (var numAndIndexesGroup in numAndIndexesGroups)
{
List<int> sortedNums = new List<int>();
List<int> sortedIndices = new List<int>();
foreach (var pair in numAndIndexesGroup)
{
sortedNums.Add(pair.Key);
sortedIndices.Add(pair.Value);
}
sortedIndices.Sort();
for (int i = 0; i < sortedNums.Count; ++i)
ans[sortedIndices[i]] = sortedNums[i];
}
return ans;
}
private KeyValuePair<int, int>[] GetNumAndIndexes(int[] nums)
{
KeyValuePair<int, int>[] numAndIndexes = new KeyValuePair<int, int>[nums.Length];
for (int i = 0; i < nums.Length; ++i)
numAndIndexes[i] = new KeyValuePair<int, int>(nums[i], i);
Array.Sort(numAndIndexes, (a, b) => a.Key.CompareTo(b.Key));
return numAndIndexes;
}
}Was this solution helpful?
Related Problems
- 4. Median of Two Sorted Arrays(Hard)
- 11. Container With Most Water(Medium)
- 15. 3Sum(Medium)
- 16. 3Sum Closest(Medium)
- 26. Remove Duplicates from Sorted Array(Easy)
- 27. Remove Element(Easy)