這里有 n 個航班分衫,它們分別從 1 到 n 進行編號。
我們這兒有一份航班預(yù)訂表言询,表中第 i 條預(yù)訂記錄 bookings[i] = [i, j, k] 意味著我們在從 i 到 j 的每個航班上預(yù)訂了 k 個座位。
請你返回一個長度為 n 的數(shù)組 answer,按航班編號順序返回每個航班上預(yù)訂的座位數(shù)宵膨。
示例:
輸入: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]