Lintcode 1897 · Meeting Room III [Python]

G家的题目确实质量高,扫描线+前缀和,尤其是前缀和的使用上考察上,很有点儿东西。首先设置时间线数组记录一个会议开始和结束,开始的地方设置+1,结束的时候-1,然后开始遍历这个时间线数组,并做前缀和计算,目的是看某一段内有多少个会议,当前缀和数字大于等于会议室数量的地方,我们在前缀和的数组里设置为1,而少于会议室数量的时候设置为0,则,两个1之间的0的持续段落就是可以放置会议的地方,随后,我们将得到的前缀和数组再次进行前缀和计算,则我们可以通过数组里,一段区间内的数字的不改变知道哪些连续的区域内可以满足放置新的会议(ask)。注意最后,ask[i][0]-1, 目的是确保这个新会议的开始位置,不会某个已有会议的开始时间,起码得是这段连续相同数字的第二位。然后ask[i][1]-1,是确保只要这段连续的位置到达的截止时间是原有某会议的开始时间之前一位数字。

class Solution:
    """
    @param intervals: the intervals
    @param rooms: the sum of rooms
    @param ask: the ask
    @return: true or false of each meeting
    """
    def meetingRoomIII(self, intervals, rooms, ask):
        # Write your code here.
        cursum = [0]*50010
        inter = [0]*50010
        res = [False]*len(ask)


        maxtime = float('-inf')
        for interv in intervals:
            inter[interv[0]]+=1
            inter[interv[1]]-=1
            maxtime = max(maxtime, interv[1])

        for a4k in ask:
            maxtime = max(maxtime, a4k[1])
        cummulation = 0
        for i in range(1, maxtime+1):
            cummulation += inter[i]
            if cummulation < rooms:
                cursum[i] = 0
            else:
                cursum[i] = 1

        for i in range(1, maxtime+1):
            cursum[i] += cursum[i-1]

        for i in range(len(ask)):
            if cursum[ask[i][0]-1] == cursum[ask[i][1]-1]:
                res[i] = True 
            else:
                res[i] = False 
        return res
        


        

1897 · Meeting Room III
Algorithms
Medium
Accepted Rate
26%

DescriptionSolutionNotesDiscussLeaderboard
Description
you have a list intervals of current meetings, and some meeting rooms with start and end timestamp.When a stream of new meeting ask coming in, judge one by one whether they can be placed in the current meeting list without overlapping…A meeting room can only hold one meeting at a time. Each inquiry is independent.

The meeting asked can be splited to some times. For example, if you want to ask a meeting for [2, 4], you can split it to [2,3] and [3, 4].

Ensure that Intervals can be arranged in rooms meeting rooms
Ensure that the end time of any meeting is not greater than 50000
|Intervals|<=50000
|ask|<=50000
rooms<=20

Example
Example 1:

Input:
Intervals:[[1,2],[4,5],[8,10]], rooms = 1, ask: [[2,3],[3,4]]
Output:
[true,true]
Explanation:
For the ask of [2,3], we can arrange a meeting room room0.
The following is the meeting list of room0:
[[1,2], [2,3], [4,5], [8,10]]
For the ask of [3,4], we can arrange a meeting room room0.
The following is the meeting list of room0:
[[1,2], [3,4], [4,5], [8,10]]
Example 2:

Input:
[[1,2],[4,5],[8,10]]
1
[[4,5],[5,6]]
Output:
[false,true]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值