2402. Meeting Rooms III

https://leetcode.com/problems/meeting-rooms-iii/description/?envType=company&envId=tiktok&favoriteSlug=tiktok-thirty-days

class Solution:
    def mostBooked(self, n: int, meetings: List[List[int]]) -> int:
        meetings.sort()
        heap=[]
        counter=defaultdict(int)

        available_rooms=[]
        for i in range(n):
            heappush(available_rooms,i)

        cnt=0
        for i in range(len(meetings)):
            while heap and heap[0][0]<=meetings[i][0]:
                h=heappop(heap)
                heappush(available_rooms,h[1])
            
            if available_rooms:
                room=heappop(available_rooms)
                heappush(heap,(meetings[i][1],room))
                counter[room]+=1
            else:
                h=heappop(heap)
                counter[h[1]]+=1
                if h[0]>meetings[i][0]:
                    diff=h[0]-meetings[i][0]
                    meetings[i][1]+=diff
                # print(h[0],h[1])
                heappush(heap,(meetings[i][1],h[1]))

        ans=0
        for k in list(counter.keys()):
            if counter[k]>counter[ans]:
                ans=k
        return ans

这里要维持两个heap

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值