Skip to content

Commit 570016d

Browse files
committed
Nov 12
1 parent 1994cc4 commit 570016d

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from bisect import bisect_right
2+
from typing import List
3+
4+
5+
class Solution:
6+
def maximumBeauty(
7+
self, items: List[List[int]], queries: List[int]
8+
) -> List[int]:
9+
# sort items by price, then reverse beauty
10+
items.sort(key=lambda i: (i[0], -i[1]))
11+
# max accumulate beauty
12+
max_beauty = items[0][1]
13+
for item in items[1:]:
14+
max_beauty = max(max_beauty, item[1])
15+
item[1] = max_beauty
16+
# extract prices for bisect
17+
prices = [item[0] for item in items]
18+
# most beautiful item for each query
19+
answer = []
20+
for query in queries:
21+
idx = bisect_right(prices, query)
22+
if idx > 0:
23+
answer.append(items[idx-1][1])
24+
else:
25+
answer.append(0)
26+
return answer
27+
28+
29+
def main():
30+
items = [[1, 2], [3, 2], [2, 4], [5, 6], [3, 5]]
31+
queries = [1, 2, 3, 4, 5, 6]
32+
assert Solution().maximumBeauty(items, queries) == [2, 4, 5, 5, 6, 6]
33+
34+
items = [[1, 2], [1, 2], [1, 3], [1, 4]]
35+
queries = [1]
36+
assert Solution().maximumBeauty(items, queries) == [4]
37+
38+
items = [[10, 1000]]
39+
queries = [5]
40+
assert Solution().maximumBeauty(items, queries) == [0]
41+
42+
43+
if __name__ == '__main__':
44+
main()

2024-11-November-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
| November 9 | [3133. Minimum Array End](https://leetcode.com/problems/minimum-array-end/) | Medium | Unsolved |
1515
| November 10 | [3097. Shortest Subarray With OR at Least K II](https://leetcode.com/problems/shortest-subarray-with-or-at-least-k-ii/) | Medium | Unsolved |
1616
| November 11 | [2601. Prime Subtraction Operation](https://leetcode.com/problems/prime-subtraction-operation/) | Medium | Unsolved |
17-
| November 12 | []() | | |
17+
| November 12 | [2070. Most Beautiful Item for Each Query](https://leetcode.com/problems/most-beautiful-item-for-each-query/) | Medium | Solved |
1818
| November 13 | []() | | |
1919
| November 14 | []() | | |
2020
| November 15 | []() | | |
@@ -38,5 +38,5 @@
3838
| Level | Problems | Solved | Unsolved |
3939
| --- | --- | --- | --- |
4040
| Easy | 3 | 3 | 0 |
41-
| Medium | 8 | 2 | 6 |
41+
| Medium | 9 | 3 | 6 |
4242
| Hard | 0 | 0 | 0 |

0 commit comments

Comments
 (0)