![]() |
VOOZH | about |
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.
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"]
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:
Below is the implementation of the above approach.
TV Laptop Phone Watch
Time Complexity : O(N * log(N)), Where N is the number of items
Space Complexity : O(N)