Most Beauty Item for each query - Easy Explanation - 🌟Beats 100%🌟
Intuition\n1. Sort the items array based on the prices and if the prices are same, sort the items based on beauty in descending order.\n\n1. Use of TreeMap for 
# Intuition\n1. Sort the items array based on the prices and if the prices are same, sort the items based on beauty in descending order.\n\n1. Use of TreeMap for efficient computation of key-value pair.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n\n**Custom sorting of items array**:\n \nSort the items based on the prices in ascending order and if the prices are same, sort the items based on beauty in descending .\n\nHence for the item with same prices , the item with maximum prices will appear first and so on..\n\n    Arrays.sort(items,(a,b)-> a[0]==b[0]? b[1]-a[1] : a[0]-b[0]);\n\n**Initialization**:\n\nInitialize the currMaxBeauty to 0 to keep track of the maximum beauty till now.\n\nInitialize TreeMap with key and value as integer for efficient storing and computation purpose.\n\n    int currMaxBeauty=0;\n    TreeMap<Integer,Integer> map=new TreeMap<>();\n\n**Storing values in TreeMap**:\n\nIterate through the whole items array \n\n    for(int[] item:items) {\n        int price=item[0];\n        int beauty=item[1];\n\ncheck if the beauty is less than currMaxBeauty,If so continue to the next iteration.\n\n    if(currMaxBeauty>=beauty)\n        continue;\n\nOtherwise , store the price and currMaxBeauty to the TreeMap as key->value pair.\n\n    currMaxBeauty=beauty;\n    map.put(price,currMaxBeauty);\n\n**Finding the maximum beauty**:\n  \n**Initialization**:\n\nInitialize no_queries with the total length of the query.\n\nanswer as an Integer array of size no_queries to store maximum beauty that has price less than or equal to queries[i].\n\n    int no_queries=queries.length;\n    int[] answer=new int[no_queries];\n\n**Iteration**:\n\nIterate through the whole queries array.\n\n    for(int i=0;i<no_queries;i++){\n\nFind entry in TreeMap with price less than or equal to price in queries array using map.floorEntry().\n\n    Map.Entry<Integer,Integer> entry=map.floorEntry(queries[i]);\n\nIf the function returns an entry, then the value at the entry is stored in the answer array.\n\n    if(entry!=null)\n    {\n        answer[i]=entry.getValue();\n    }\n\n\nIf it is null, then in the answer array, the result for the query is stored as 0.\n\n    else\n    {\n        answer[i]=0;\n    }\n\n**Returning the value**:\n\n   Return answer array which contains the result for each query if exists .Otherwise, it\'s zero.\n\n    return answer;\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- ***Time complexity***: **O(nlogn + (m+n) log k)**. \n    n -> number of items.\n    m -> number of queries.\n    k -> number of unique prices.\n\n**Sorting** : **O(nlogn)**.\n\n   The first step sorts the items array by price in ascending order, and if two items have the same price, it sorts by beauty in descending order.Sorting of n items take **O(nlogn) time**.\n\n**Building the TreeMap**: **O(nlogk)**.\n\n   Inserting the TreeMap takes **O(log k) times** , k is the number of unique prices in the items array.\n\n   Thus loop runs in **O(nlogk) time** in worst case for n items.\n\n**Processing Queries**: **O(mlogk)**.\n\nEach query involves a floorEntry lookup in the TreeMap, which takes **O(logk) time**.\n\nWith m queries, this loop runs in **O(mlogk) time**.\n\n**Overall Time Complexity**: **O(nlogn + (n+m) logk)**.\n\n  Combining the above steps, the overall time complexity is **O(nlogn)+O(nlogk)+O(mlogk)**.\n\n   Since **O(nlogn)** is generally the dominant term when sorting a large array, the overall time complexity simplifies to **O(nlogn+(n+m)logk)**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- ***Space complexity***: **O(n+m)**.\n    n -> number of items.\n    m -> number of queries.\n\n**TreeMap Storage**: **O(n)**.\n\nThe TreeMap stores at most n entries (if each item has a unique price). This takes **O(n) space**.\n\n**Answer array**: **O(m)**.\n\nThe answer array stores m integers, taking **O(m) space**.\n\n**Overall Space Complexity**: **O(n+m)**.\n  Hence the overall space complexity:\n    **O(n) + O(m) = O(n+m)**.\n\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\npublic class Solution {\n    public int[] maximumBeauty(int[][] items, int[] queries) {\n        // Sort items by price ,\n        // if they have same price -> then sort by beauty in descending order\n\n        Arrays.sort(items,(a,b)-> a[0]==b[0]? b[1]-a[1] : a[0]-b[0]);\n        int currMaxBeauty=0;\n\n\n        TreeMap<Integer,Integer> map=new TreeMap<>();\n        for(int[] item:items)\n        {\n            int price=item[0];\n            int beauty=item[1];\n\n            if(currMaxBeauty>=beauty)\n              continue;\n            currMaxBeauty=beauty;\n            map.put(price,currMaxBeauty);\n            \n        }\n\n         int no_queries=queries.length;\n        int[] answer=new int[no_queries];\n\n        for(int i=0;i<no_queries;i++)\n        {\n            Map.Entry<Integer,Integer> entry=map.floorEntry(queries[i]);\n            if(entry!=null)\n            {\n                answer[i]=entry.getValue();\n            }\n            else\n            {\n                answer[i]=0;\n            }\n        }\n        return answer;\n        \n    }\n}\n```
['Array', 'Sorting', 'Java']