刷题-力扣-1109. 航班预订统计

1109. 航班预订统计

题目链接

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/corporate-flight-bookings
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题目描述

这里有 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
预订记录 1 :   10  10
预订记录 2 :       20  20
预订记录 3 :       25  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 * 104
  • 1 <= bookings.length <= 2 * 104
  • bookings[i].length == 3
  • 1 <= firsti <= lasti <= n
  • 1 <= seatsi <= 104

题目分析

  1. 根据题目描述,求各个航班上座位预定的信息
  2. 使用差分数组实现。差分数组的前i位的和表示原数组第i位的真实值。例如,原数组为[10, 55, 45, 25, 25],差分数组为[10, 45, -10, -20, 0]
  3. 航班预订表 bookings中的每一条数据都表示航班范围的座位增加,即first-last座位增加seat,表示为差分数组即first增加seat,last+1减少seat
  4. 依次遍历bookings得出航班座位的预定信息差分表,再将差分数组转换为原数组

代码

class Solution {
public:
    vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
        vector<int> answer(n, 0);
        for (auto booking : bookings) {
            answer[booking[0] - 1] += booking[2];
            if (booking[1] < n) answer[booking[1]] -= booking[2];
        }
        for (int i = 1; i < n; ++i) {
            answer[i] += answer[i - 1];
        }
        return answer;
    }
};
原文地址:https://www.cnblogs.com/HanYG/p/15210402.html