zoukankan      html  css  js  c++  java
  • [LeetCode] 1109. 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:

    Input: bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
    Output: [10,55,45,25,25]
    Explanation:
    Flight labels:        1   2   3   4   5
    Booking 1 reserved:  10  10
    Booking 2 reserved:      20  20
    Booking 3 reserved:      25  25  25  25
    Total seats:         10  55  45  25  25
    Hence, answer = [10,55,45,25,25]
    

    Example 2:

    Input: bookings = [[1,2,10],[2,2,15]], n = 2
    Output: [10,25]
    Explanation:
    Flight labels:        1   2
    Booking 1 reserved:  10  10
    Booking 2 reserved:      15
    Total seats:         10  25
    Hence, answer = [10,25]
    

    Constraints:

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

    航班预订统计。

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

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

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

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

    题意是给一个叫做 bookings 的二维数组,里面包含了一些 [i, j, k] 的子数组,分别表示一些航班的预订情况,从 i 到 j 航班,有 K 个预订。同时给了一个变量 N 表示航班的数量。请输出一个数组表示所有航班的预订情况。

    这里我给出两种方法。首先是暴力解。对于每一个 booking,因为给了起始航班 i,结束航班 j 和人数 k,所以对于 [i, j] 这些航班,我们对 res[i] 累加 k 即可。注意最后结果集里面下标是不包含 0 的,所以需要重新写一份。

    时间O(n^2) - worst case

    空间O(n)

    Java实现

     1 class Solution {
     2     public int[] corpFlightBookings(int[][] bookings, int n) {
     3         int[] temp = new int[n + 1];
     4         for (int[] book : bookings) {
     5             int start = book[0];
     6             int end = book[1];
     7             int seat = book[2];
     8             for (int i = start; i <= end; i++) {
     9                 temp[i] += seat;
    10             }
    11         }
    12         int[] res = new int[n];
    13         for (int i = 1; i < temp.length; i++) {
    14             res[i - 1] = temp[i];
    15         }
    16         return res;
    17     }
    18 }

    其次是一个类似扫描线的方法。当你拿到一个 booking = [i, j, k] 的时候,意味着在 [0, i) 和 (j, n] 这些航班,是没有这些乘客的。对于第 i 个航班来说,他比第 i - 1 个航班要多 k 个乘客;然后对于 j 之后的航班,他们又失去了这些乘客。所以我们可以创建一个 counter 数组,用累加的方式记录航班之间的乘客数量的变化。

    遍历每个 booking(b),在 counter[b[0] - 1]上 += b[2],说明从第 b[0] 个航班,突然比之前所有的航班都多了 k 个乘客;同时如果 b[1] < n(b[1]的下标不能越界),在 counter[b[1]] -= b[2],说明从 b[1] 个航班开始,突然比之前所有的航班都少了 k 个乘客。

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public int[] corpFlightBookings(int[][] bookings, int n) {
     3         int[] counters = new int[n];
     4         for (int[] b : bookings) {
     5             counters[b[0] - 1] += b[2];
     6             if (b[1] < n) {
     7                 counters[b[1]] -= b[2];
     8             }
     9         }
    10         for (int i = 1; i < n; i++) {
    11             counters[i] += counters[i - 1];
    12         }
    13         return counters;
    14     }
    15 }

    相关题目

    1094. Car Pooling

    1109. Corporate Flight Bookings

    扫描线相关题目

    LeetCode 题目总结

  • 相关阅读:
    指针作为函数参数
    二级指针与多级指针
    指针数组与数组指针
    指针与数组
    指针引入
    Python中调用自然语言处理工具HanLP手记
    HanLP中的人名识别分析详解
    自然语言处理中的分词问题总结
    Hanlp实战HMM-Viterbi角色标注中国人名识别
    Hanlp中使用纯JAVA实现CRF分词
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13683176.html
Copyright © 2011-2022 走看看