LeetCode 每日一题 1109. 航班预订统计

483 阅读2分钟

这是我参与8月更文挑战的第31天,活动详情查看:8月更文挑战

1109. 航班预订统计

这里有 n 个航班,它们分别从 1 到 n 进行编号。

有一份航班预订表 bookings ,表中第 i 条预订记录 bookings[i] = [firsti, lasti, seatsi]意味着在从 firsti 到 lasti (包含 firsti 和 lasti )的 每个航班 上预订了 seatsi 个座位。

请你返回一个长度为 n 的数组 answer,其中 answer[i] 是航班 i 上预订的座位总数。

示例 1:

输入: bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
输出: [10,55,45,25,25]
解释:
航班编号        1   2   3   4   5
预订记录 110  10
预订记录 220  20
预订记录 325  25  25  25
总座位数:      10  55  45  25  25
因此,answer = [10,55,45,25,25]

示例 2:

输入: bookings = [[1,2,10],[2,2,15]], n = 2
输出: [10,25]
解释:
航班编号        1   2
预订记录 1 :   10  10
预订记录 2 :       15
总座位数:      10  25
因此,answer = [10,25]

提示:

  • 1 <= n <= 2 * 10^4
  • 1 <= bookings.length <= 2 * 10^4
  • bookings[i].length == 3
  • 1 <= firsti <= lasti <= n
  • 1 <= seatsi <= 10^4

方法一

差分:

根据题意描述,我们可以化简为如下要求:

1~n的坐标轴上,给某个连续区间上的数都加上某个数x;做完k次这样的操作后,问,最后坐标轴上每个数的大小;

模型抽象出来后,我们就可以看到,这是典型的一道差分题;

例如有这样一个操作:给1~5中的每个数都加上2;那么,我们可以给坐标为1的先加上2,接着给坐标为6的减去2;为什么要这么做呢?因为,我们最后求某个位置上的数时,求的是他的前缀和;我们给1加上2,那么求前缀和时,相当于1后面的数都加上的2;所以,为了只在1~5之间加上2,我们得在6的位置减去2

上述的操作正好对应题目中的[firsti, lasti, seatsi]

class Solution {
    public int[] corpFlightBookings(int[][] bookings, int n) {
        int[] res = new int[n];
        for (int[] x : bookings) {
            int st = x[0], ed = x[1], cnt = x[2];
            res[st - 1] += cnt;
            if (ed < n)
                res[ed] -= cnt;
        }

        for (int i = 1; i < n; i ++ )
            res[i] += res[i - 1];
        return res;
    }
}

时间复杂度: O(n)

空间复杂度: O(1)