[JavaScript] 2070. Most Beautiful Item for Each Query
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n. binary search in items for each queryPrice\n\n# Complexity\n- Time comp
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n. binary search in `items` for each `queryPrice`\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n\n---\n\n```\nvar maximumBeauty = function (items, queries) {\n  items.sort((a, b) => a[0] - b[0]);\n  let maxBeauty = 0;\n  let prefixMaxBeauty = [];\n  for (let [price, beauty] of items) {\n    prefixMaxBeauty.push((maxBeauty = Math.max(maxBeauty, beauty)));\n  }\n  let answer = [];\n  for (let queryPrice of queries) {\n    let low = 0;\n    let high = items.length - 1;\n    while (low <= high) {\n      let mid = ~~(low / 2 + high / 2);\n      if (items[mid][0] <= queryPrice) {\n        low = mid + 1;\n      } else {\n        high = mid - 1;\n      }\n    }\n    answer.push(high >= 0 ? prefixMaxBeauty[high] : 0);\n  }\n  return answer;\n};\n```\n\n![image.png](https://assets.leetcode.com/users/images/b3d2a698-e458-4ac7-a008-d404db2375ab_1701446557.7676525.png)\n\n---\n
['JavaScript']