Beats 100% simple code using Sorting and Forloop
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires finding the maximum beauty for each query, where beauty values are
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires finding the maximum beauty for each query, where beauty values are associated with items of certain prices. To achieve this efficiently, we should consider sorting the items by their beauty values in descending order. This way, we can iterate through the items and quickly find the maximum beauty available within a given price constraint.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Sort Items by Beauty:** First, sort the items array by beauty values in descending order. This allows us to prioritize higher beauty values when searching for an item that meets the price constraint of each query.\n\n2. **Process Each Query:** For each query:\nInitialize maxi to 0. This variable stores the maximum beauty for items within the price limit of the current query.\nTraverse the sorted items array and check if an item\u2019s price is less than or equal to the query price. If so, set maxi to the beauty value of that item (since it is the highest beauty value encountered so far) and break out of the loop.\n\n3. **Store Results:** For each query, store the maximum beauty found in the ans array and return it as the result.\n\n# Complexity\n- Time Complexity:\nSorting the `items` array takes \\( O(m \\log m) \\), where \\( m \\) is the number of items. For each query, we might traverse all items, resulting in a time complexity of \\( O(m + q \\cdot m) \\), where \\( q \\) is the number of queries. Future optimizations, like binary search, could reduce the inner loop complexity.\n\n- Space Complexity:\nWe use \\( O(q) \\) additional space to store the `ans` array, where \\( q \\) is the number of queries. Other than this, there\u2019s no significant extra auxiliary space required.\n\n# Code\n```java []\nclass Solution {\n    public int[] maximumBeauty(int[][] items, int[] queries) {\n        Arrays.sort(items,(a,b)-> b[1]-a[1]);\n        int ans[] = new int[queries.length];\n        for(int i=0;i<queries.length;i++){\n            int maxi=0;\n            for(int j=0;j<items.length;j++){\n                if(items[j][0]<= queries[i]){\n                    maxi = items[j][1];\n                    break;\n                }\n            }\n            ans[i] = maxi;\n        }\n        return ans;\n    }\n}\n```\n``` python []\ndef maximumBeauty(items, queries):\n    items.sort(key=lambda x: -x[1])\n    ans = []\n\n    for query in queries:\n        maxi = 0\n        for price, beauty in items:\n            if price <= query:\n                maxi = beauty\n                break\n        ans.append(maxi)\n    \n    return ans\n```\n``` c++ []\n#include <algorithm>\n#include <vector>\nusing namespace std;\n\nclass Solution {\npublic:\n    vector<int> maximumBeauty(vector<vector<int>>& items, vector<int>& queries) {\n        sort(items.begin(), items.end(), [](vector<int>& a, vector<int>& b) {\n            return b[1] - a[1];\n        });\n\n        vector<int> ans(queries.size());\n        \n        for (int i = 0; i < queries.size(); i++) {\n            int maxi = 0;\n            for (const auto& item : items) {\n                if (item[0] <= queries[i]) {\n                    maxi = item[1];\n                    break;\n                }\n            }\n            ans[i] = maxi;\n        }\n        \n        return ans;\n    }\n};\n```\n``` javascript []\nfunction maximumBeauty(items, queries) {\n    items.sort((a, b) => b[1] - a[1]);\n    let ans = [];\n\n    for (let query of queries) {\n        let maxi = 0;\n        for (let [price, beauty] of items) {\n            if (price <= query) {\n                maxi = beauty;\n                break;\n            }\n        }\n        ans.push(maxi);\n    }\n\n    return ans;\n}\n```
['Array', 'Binary Search', 'Sorting', 'Python', 'C++', 'Java', 'JavaScript']