✅Simple Loop Solution Beats 100% 🔥35ms 🎯
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nC++\n\n\nPython\n\n\n\n
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nC++\n![image.png](https://assets.leetcode.com/users/images/aa3bd3b7-7ace-49b7-b470-c12c225c2e9b_1731426592.8866725.png)\n\nPython\n![image.png](https://assets.leetcode.com/users/images/b2f024f2-ae51-4d1c-81ca-385947a6a180_1731426559.6690147.png)\n\n\n# Complexity\n- Time complexity:\n$$O(n^2)$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    vector<int> maximumBeauty(vector<vector<int>>& items, vector<int>& queries) {\n        int maxi = INT_MAX;\n\n        sort(items.begin(), items.end());\n\n        vector<vector<int>> res = {{0, 0, maxi}}; // minprice beauty maxprice\n\n        for(auto& item : items){\n            int price = item[0];\n            int beauty = item[1];\n            if(beauty > res.back()[1]){\n                if(price == res.back()[0]){\n                    res.back()[1] = beauty;\n                }\n                else{\n                    res.back()[2] = price;\n                    res.push_back({price, beauty, maxi});\n                }\n            }\n        }\n\n        vector<int> ans;\n\n        for(int q : queries){\n            for(int i = res.size() - 1; i >= 0; i--){\n                if(res[i][0] <= q){\n                    ans.push_back(res[i][1]);\n                    break;\n                }\n            }\n        }\n\n        return ans;\n    }\n};\n```\n```python3 []\nclass Solution:\n    def maximumBeauty(self, items: List[List[int]], queries: List[int]) -> List[int]:\n        \n        maxi = float(\'inf\')\n        res = [[0, 0, maxi]] # minprice, beauty, maxprice\n**Bold**\n        items.sort(key = lambda x: x[0])\n\n        for price, beauty in items:\n            \n            if beauty > res[-1][1]:\n                if price == res[-1][0]:\n                    res[-1][1] = beauty\n                else:\n                    res[-1][2] = price\n                    res.append([price, beauty, maxi])\n\n        ans = []\n\n        for q in queries:\n            for i in range(len(res) - 1, -1, -1):\n                if res[i][0] <= q:\n                    ans.append(res[i][1])\n                    break\n        \n        return ans\n```
['Array', 'Sorting', 'C++', 'Python3']