时间分隔(车站到站)

Description
Given arrival and departure times of all trains that reach a railway station. Your task is to find the minimum number of platforms required for the railway station so that no train waits.

Note: Consider that all the trains arrive on the same day and leave on the same day. Also, arrival and departure times must not be same for a train.

Input
The first line of input contains T, the number of test cases. For each test case, first line will contain an integer N, the number of trains. Next two lines will consist of N space separated time intervals denoting arrival and departure times respectively.

Note: Time intervals are in the 24-hourformat(hhmm), preceding zeros are insignificant. 200 means 2:00.
Consider the example for better understanding of input.

Constraints:1 <= T <= 100,1 <= N <= 1000,1 <= A[i] < D[i] <= 2359

Output
For each test case, print the minimum number of platforms required for the trains to arrive and depart safely.

Sample Input 1
1
6
900 940 950 1100 1500 1800
910 1200 1120 1130 1900 2000

Sample Output 1
3

# 到站数组、出站数组、车辆数
def solution(l1, l2, number):
    # 不用管到站、出战的是哪辆车,只要关注时间点就可以
    # 某个时间点有车进站,必须要有站台,某个时间点出战,可以腾出站台来
    l1.sort()
    l2.sort()
    # 实时车站需要量
    plat_need = 1
    # 最大需要量
    result = 1
    # i表示第几个进站,j表示第几个出站
    i = 1
    j = 0
    while i < number and j < number:
        # 下一辆车的到站时间比前一辆车的出站时间早,站台数+1
        if l1[i] < l2[j]:
            plat_need += 1
            i += 1
            if plat_need > result:
                result = plat_need
        else:
            # 当l1[i] == l2[j]时,先出站,plat_need-1,执行到下次while循环时再进站
            plat_need -= 1
            j += 1
    return result


if __name__ == '__main__':
    n = int(input())
    for i in range(n):
        number = int(input())
        l1 = list(map(int, input().strip().split()))
        l2 = list(map(int, input().strip().split()))
        result = solution(l1, l2, number)
        print(result)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值