Beats 95% || C++ || Sorting || Offline Queries
Intuition\n Describe your first thoughts on how to solve this problem. \nTo maximize beauty for each query efficiently, sorting both the queries and the items b
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo maximize beauty for each query efficiently, sorting both the queries and the items by price in ascending order is key. By doing this, we can iterate through items and update the maximum beauty value for all queries whose price threshold has been reached.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.\tPair each query with its original index and sort the queries by their price.\n2.\tSort the items by price.\n3.\tUse a two-pointer approach: iterate over the items, updating the maximum beauty whenever the item price is less than or equal to the query price.\n4.\tStore the results in the answer array at the corresponding indices from the original queries.\n# Complexity\n- Time complexity: $$O(n*log(n) + m*log(m))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n+m)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    vector<int> maximumBeauty(vector<vector<int>>& items, vector<int>& queries) {\n        int n = items.size(), m = queries.size();\n        vector<pair<int,int>> vp;\n        for(int j=0; j<m; j++){\n            vp.push_back({queries[j], j});\n        }\n        sort(vp.begin(), vp.end());\n        sort(items.begin(), items.end());\n\n        int maxi = 0, i = 0;\n        vector<int> ans(m);\n        for(auto it: vp){\n            int price = it.first, ind = it.second;\n            while(i<n && items[i][0] <= price){\n                maxi = max(maxi, items[i][1]);\n                i++;\n            }\n            ans[ind] = maxi;\n        }\n        return ans;\n    }\n};\n```
['Array', 'Sorting', 'C++']