LeetCode 2948: Make Lexicographically Smallest Array by Swapping Elements Solution

Master LeetCode problem 2948 (Make Lexicographically Smallest Array by Swapping Elements), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2948. Make Lexicographically Smallest Array by Swapping Elements

Problem Explanation

Explanation

To solve this problem, we can use a priority queue to store the elements in a sorted order. We iterate through the array and for each element, we try to find the smallest element within the limit range in the priority queue. If such an element is found, we swap it with the current element to make the array lexicographically smaller.

The algorithmic steps are as follows:

  1. Initialize an empty priority queue sorted based on element value along with their indices.
  2. Iterate through the array elements.
  3. For each element, check the smallest element within the limit range in the priority queue.
  4. If such an element exists, swap it with the current element.
  5. Continue this process until no more swaps can be made.

At each step, we are trying to make the array lexicographically smaller by performing the allowed swaps. This way, we can obtain the lexicographically smallest array possible.

Time Complexity: The time complexity of this approach is O(n log n) where n is the number of elements in the input array nums. This is due to the iteration through the array and the operations on the priority queue.

Space Complexity: The space complexity is O(n) to store the priority queue.

Solution Code

import java.util.*;

class Solution {
    public int[] minArray(int[] nums, int limit) {
        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);
        
        for (int i = 0; i < nums.length; i++) {
            pq.offer(new int[]{nums[i], i});
        }
        
        while (!pq.isEmpty()) {
            int[] curr = pq.poll();
            int diff = curr[0];
            int index = curr[1];
            
            for (int i = index + 1; i < nums.length; i++) {
                if (Math.abs(nums[i] - diff) <= limit) {
                    int temp = nums[i];
                    nums[i] = diff;
                    nums[index] = temp;
                    break;
                }
            }
        }
        
        return nums;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2948 (Make Lexicographically Smallest Array by Swapping Elements)?

This page provides optimized solutions for LeetCode problem 2948 (Make Lexicographically Smallest Array by Swapping Elements) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2948 (Make Lexicographically Smallest Array by Swapping Elements)?

The time complexity for LeetCode 2948 (Make Lexicographically Smallest Array by Swapping Elements) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2948 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2948 in Java, C++, or Python.

Back to LeetCode Solutions