1109. Corporate Flight Bookings(M)
https://leetcode.com/problems/corporate-flight-bookings/
There are n
flights that are labeled from 1
to n
.
You are given an array of flight bookings bookings
, where bookings[i] = [firsti, lasti, seatsi]
represents a booking for flights firsti
through lasti
(inclusive) with seatsi
seats reserved for each flight in the range.
Return an array answer
of length n
, where answer[i]
is the total number of seats reserved for flight i
.
Example 1:
Example 2:
Constraints:
1 <= n <= 2 * 104
1 <= bookings.length <= 2 * 104
bookings[i].length == 3
1 <= firsti <= lasti <= n
1 <= seatsi <= 104
Solution:
Solution:
这个题目就在那绕弯弯,其实它就是个差分数组的题,我给你翻译一下:
给你输入一个长度为 n
的数组 nums
,其中所有元素都是 0。再给你输入一个 bookings
,里面是若干三元组 (i,j,k)
,每个三元组的含义就是要求你给 nums
数组的闭区间 [i-1,j-1]
中所有元素都加上 k
。请你返回最后的 nums
数组是多少?
PS:因为题目说的
n
是从 1 开始计数的,而数组索引从 0 开始,所以对于输入的三元组(i,j,k)
,数组区间应该对应[i-1,j-1]
。
这么一看,不就是一道标准的差分数组题嘛?我们可以直接复用刚才写的类:
这道题就解决了。
Last updated