作者dont (dont)
標題Re: [閒聊] 每日leetcode
時間2024-11-12 19:04:44
2070. Most Beautiful Item for Each Query
## 思路
1. 對items排序
2. 更新items的beauty (紀錄目前為止的max beauty)
3. Binary Search
## Code
```python
class Solution:
def maximumBeauty(self, items: List[List[int]], queries: List[int]) ->
List[int]:
items.sort()
n = len(items)
for i in range(1, n):
items[i][1] = max(items[i-1][1], items[i][1])
def get_beauty(query):
left, right = 0, n-1
res = 0
while left <= right:
mid = (left + right) // 2
if items[mid][0] <= query:
res = max(res, items[mid][1])
left = mid + 1
elif items[mid][0] > query:
right = mid - 1
return res
return [get_beauty(q) for q in queries]
```
--
※ 發信站: 批踢踢實業坊(web-ptt.org.tw), 來自: 185.213.82.158 (臺灣)
※ 文章網址: https://web-ptt.org.tw/Marginalman/M.1731409487.A.EB7