Java Program to Sort Items By Weight
Given two array items and weights which denotes items and their respective weights. Both arrays are of equal length. For every index ‘i’, items[i] and weights[i] represent the ith item name and its weight respectively. Your task is to return a new array of items sorted in decreasing order by their weights.
Note: Each item has a unique positive integer weight.
Examples of Sorting Items by Weight
Input: items = [“Laptop”, “TV”, “Phone”, “Watch”]
weights = [500, 1000,250, 50]
Output : [“TV”, “Laptop”, “Phone”, “Watch”]
Explanation: Here the items are sorted based on their weight in decreasing orderInput: items = [“Banana”, “Mango”, “Apple”]
weights = [50, 100, 60]
Output : [“Mango”, “Apple”, “Banana”]
Sort Items By Weight by using the Priority Queue
The idea is to put all the items along with their respective weights into a Priority queue and then sort items in decreasing order by their weights using a custom comparator.
Follow the steps given below to implement the approach:
- Create a class named ‘Pair’ that consists of the item name and its respective weight
- Initialize a priority queue and use a custom comparator to sort the pairs based on the weight attribute in descending order.
- Iterate through the items and weights arrays.
- Create a Pair object for each item with their respective weight and then add them to the priority queue.
- Initialize a string array named ‘ans’ with the same length as the ‘items’ array.
- Take a variable named ‘idx’ which will keep track of the current position in the ‘ans’ array
- Now iterate through the priority queue while it is not empty. In each iteration, it removes a ‘Pair’ from the priority queue which is nothing but the item with the highest weight. Store the item in the ‘ans’ array at the ‘idx’ position. Then, increment the ‘idx’ for the next iteration.
- Finally, return the ‘ans’ array which now contains the items sorted in decreasing order by their weights.
Below is the implementation of the above approach.
Java
// Java Program to Sort Items // By Weight import java.io.*; import java.util.PriorityQueue; // Driver Class public class SortByWeight { // Pair Class // Used to bind item and weight // together public static class Pair { String item; int weight; Pair(String item, int weight) { this .item = item; this .weight = weight; } } public static String[] sort(String[] items, int [] weights) { // initialize a priority queue // it use a custom comparator to sort the pairs // based on the weight // attribute in descending order PriorityQueue<Pair> pq = new PriorityQueue<>( (a, b) -> Integer.compare(b.weight, a.weight)); // add pair in pq for ( int i = 0 ; i < items.length; i++) { pq.add( new Pair(items[i], weights[i])); } // initialize ans array String[] ans = new String[items.length]; // initialize idx int idx = 0 ; // remove pair and place in ans array // while priority queue is not empty while (!pq.isEmpty()) { // remove pair from pq Pair p = pq.remove(); // add item in ans array ans[idx] = p.item; idx++; } return ans; } // main function public static void main(String[] args) { String[] items = { "Laptop" , "TV" , "Phone" , "Watch" }; int [] weight = { 500 , 1000 , 250 , 50 }; String[] ans = sort(items, weight); for (String x : ans) { System.out.print(x + " " ); } } } |
TV Laptop Phone Watch
Complexity of the above program:
Time Complexity : O(N * log(N)), Where N is the number of items
Space Complexity : O(N)