没有合适的资源?快使用搜索试试~ 我知道了~
python笔试题-求航班预订座位号 python笔试题-求航班预订座位号 这⾥有 n 个航班,它们分别从 1 到 n 进⾏编号。 我们这⼉有⼀份航班预订表,表中第 i 条预订记录 bookings[i] = [i, j, k] 意味着我们在从 i 到 j 的每个航班上预订了 k 个座位。 请你返回⼀个长度为 n 的数组 answer,按航班编号顺序返回每个航班上预订的座位数。 ⽰例: 输⼊:bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5 输出:[10,55,45,25,25] 提⽰: 1 <= bookings.length <= 20000 1 <= bookings[i][0] <= bookings[i][1] <= n <= 20000 1 <= bookings[i][2] <= 10000 bookings = [[1, 2, 10], [2, 3, 20], [2, 5, 25]] n = 5 def get_results(bookings, n): seat_list = {} if len(bookings) < 1
资源推荐
资源详情
资源评论
python笔试题-求航班预订座位号python笔试题-求航班预订座位号
这⾥有 n 个航班,它们分别从 1 到 n 进⾏编号。
我们这⼉有⼀份航班预订表,表中第 i 条预订记录 bookings[i] = [i, j, k] 意味着我们在从 i 到 j 的每个航班上预订了 k 个座位。
请你返回⼀个长度为 n 的数组 answer,按航班编号顺序返回每个航班上预订的座位数。
⽰例:
输⼊:bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
输出:[10,55,45,25,25]
提⽰:
1 <= bookings.length <= 20000
1 <= bookings[i][0] <= bookings[i][1] <= n <= 20000
1 <= bookings[i][2] <= 10000
bookings = [[1, 2, 10], [2, 3, 20], [2, 5, 25]]
n = 5
def get_results(bookings, n):
seat_list = {}
if len(bookings) < 1 or len(bookings) > 2000:
raise Exception("bookings.length is invalid")
for booking in bookings:
i = booking[0]
j = booking[1]
k = booking[2]
if i < 1 or i > j or j > n or j > 20000 or k < 1 or k > 1000:
raise Exception("booking {} is invalid".format(booking))
for count in range(i, j + 1):
if not seat_list.get(count):
seat_list[count] = 0
seat_list[count] = seat_list[count] + k
results = []
for num in range(n):
results.append(seat_list.get(num + 1, 0))
return results
print(get_results(bookings, n))
# [10, 55, 45, 25, 25]
# [10, 55, 45, 25, 25]
资源评论
是空空呀
- 粉丝: 167
- 资源: 3万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功