R4-贪心篇
结束时间升序排列+优先队列
class Solution:
def maxEvents(self, events: List[List[int]]) -> int:
dict=defaultdict(list)
for i,val in enumerate(events):
dict[val[0]].append(val[1])
#优先队列(小根堆)
h=[]
ret=0
for i in range(1,100001):
for end in dict[i]:
heapq.heappush(h,end)
#优化字典(删除对应的键值对)
del dict[i]
while h:
#弹出结束时间比当前开始时间早的
cur =heapq.heappop(h)
if cur>=i:
ret+=1
break
if not dict and not h:
break
return ret