Algorithm


Problem Name: 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
 

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
    public int[] corpFlightBookings(int[][] bookings, int n) {
        int[] ans = new int[n];

        for (int[] booking : bookings) {
            int tickets = booking[2];
            int from = booking[0];
            int to = booking[1];

            ans[from - 1] += tickets;
            if (to  <  n) {
                ans[to] -= tickets;
            }
        }

        int currSum = 0;
        for (int i = 0; i  <  n; i++) {
            currSum += ans[i];
            ans[i] = currSum;
        }

        return ans;
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5

Output

x
+
cmd
[10,55,45,25,25]

#2 Code Example with Javascript Programming

Code - Javascript Programming


const corpFlightBookings = function(bookings, n) {
  const arr = Array(n + 2).fill(0)
  for(const [s, e, num] of bookings) {
    arr[s] += num
    arr[e + 1] -= num
  }
  for(let i = 1; i  <  n + 2; i++) {
    arr[i] += arr[i - 1]
  }
  arr.pop()
  arr.shift()
  return arr
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5

Output

x
+
cmd
[10,55,45,25,25]

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:
        res = [0] * n
        i = cur = 0
        for j, val in sorted([[i - 1, k] for i, j, k in bookings] + [[j, -k] for i, j, k in bookings]):
            while i < j:
                res[i] = cur
                i += 1
            cur += val
        return res
Copy The Code & Try With Live Editor

Input

x
+
cmd
bookings = [[1,2,10],[2,2,15]], n = 2

Output

x
+
cmd
[10,25]
Advertisements

Demonstration


Previous
#1108 Leetcode Defanging an IP Address Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#1110 Leetcode Delete Nodes And Return Forest Solution in C, C++, Java, JavaScript, Python, C# Leetcode