Super Simple Beginner Friendly Java/C SolutionO(n(logn)+k(logn))
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n1. Sort the items array
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Sort the items array using 1st elem(price) and if 1st elem is same then sort according to 2nd elem(beauty) in ascending order to use binary search.\n2. Store the maxBeauty for each price(say two items are {1,3} and {1,5} the maxPrice will hold beauty 5 for price 1)\n3. Simply binary search to find the best beautiful item.\n# Complexity\n- Time complexity:O(nlogn+klogn). {k is size of query and n is size of items}\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n+k)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int[] maximumBeauty(int[][] items, int[] queries) {\n        Arrays.sort(items, (a, b) -> {\n            if(a[0]==b[0]) return a[1]-b[1];\n            return a[0] - b[0];\n        });\n        int n = items.length;\n        int[] maxBeauty = new int[n];\n        maxBeauty[0] = items[0][1];\n        for (int i = 1; i < n; i++) {\n            maxBeauty[i] = Math.max(maxBeauty[i - 1], items[i][1]);\n        }\n\n        int[] res = new int[queries.length];\n        for (int i = 0; i < queries.length; i++) {\n            int q = queries[i];\n            int left = 0, right = n - 1;\n            while (left <= right) {\n                int mid = (left + right) / 2;\n                if (items[mid][0] <= q) left = mid + 1;\n                else right = mid - 1;\n            }\n            res[i] = right >= 0 ? maxBeauty[right] : 0;\n        }\n\n        return res;\n    }\n}\n```\n```C []\nint compare(const void *a, const void *b) {\n    int *item1 = *(int **)a;\n    int *item2 = *(int **)b;\n    if (item1[0] == item2[0]) return item1[1] - item2[1];\n    return item1[0] - item2[0];\n}\n\nint* maximumBeauty(int** items, int itemsSize, int* itemsColSize, int* queries, int queriesSize, int* returnSize) {\n    qsort(items, itemsSize, sizeof(int*), compare);\n\n    int* maxBeauty = (int*)malloc(itemsSize * sizeof(int));\n    maxBeauty[0] = items[0][1];\n    for (int i = 1; i < itemsSize; i++) {\n        maxBeauty[i] = (maxBeauty[i - 1] > items[i][1]) ? maxBeauty[i - 1] : items[i][1];\n    }\n\n    int* res = (int*)malloc(queriesSize * sizeof(int));\n    *returnSize = queriesSize;\n    \n    for (int i = 0; i < queriesSize; i++) {\n        int q = queries[i];\n        int left = 0, right = itemsSize - 1;\n        while (left <= right) {\n            int mid = (left + right) / 2;\n            if (items[mid][0] <= q) left = mid + 1;\n            else right = mid - 1;\n        }\n        res[i] = (right >= 0) ? maxBeauty[right] : 0;\n    }\n\n    free(maxBeauty);\n    return res;\n}\n```
['Binary Search', 'C', 'Sorting', 'Java']