4 ms -> 100% | 91.44 MB -> 95.95% | Readable with Comments (map)
\n\n# Code\ncpp []\nclass Solution {\npublic:\n    vector<int> maximumBeauty(vector<vector<int>>& items, vector<int>& queries) {\n        int itemsLen = items.s
![ev22.png](https://assets.leetcode.com/users/images/d36664fd-16e5-418e-9e02-5b08e7064f8f_1731425203.272848.png)\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    vector<int> maximumBeauty(vector<vector<int>>& items, vector<int>& queries) {\n        int itemsLen = items.size();\n        int queriesLen = queries.size();\n\n        map<int, int> beautyMap;\n\n        int i = 0;\n        std::map<int, int>::iterator it;\n\n        for (; i < itemsLen; ++i) {\n            // We try to find in map element with lower price\n            it = beautyMap.upper_bound(items[i][0]);\n            it = (it != beautyMap.begin()) ? --it : it;\n\n            // Then we add it to map if the price we found is bigger\n            // might happen when there is only 1 element in map\n            // or we add when the beauty is higher\n            if (it->first > items[i][0] || it->second < items[i][1]) {\n                beautyMap[items[i][0]] = items[i][1];\n\n                // after adding the item to map we iterate over the \n                // map and update the beauty for higher prices\n                // with higher beauties of lower prices\n                for (it = std::next(beautyMap.begin()); it != beautyMap.end(); ++it) {\n                    if (std::prev(it)->second > it->second)\n                        it->second = std::prev(it)->second;\n                }\n            }\n        }\n\n        // we change the values in queries to beauties you can \n        // buy for those prices by ussing upper_bound and --it\n        // to find the beauty we can get with that price\n        for (i = 0; i < queriesLen; ++i) {\n            if (queries[i] < beautyMap.begin()->first) queries[i] = 0;\n            else {\n                it = beautyMap.upper_bound(queries[i]);\n                --it;\n                queries[i] = it->second;\n            }\n        }\n\n        return queries;\n    }\n};\n```
['C++']