1109. Corporate Flight Bookings LeetCode Solution

In this guide, you will get 1109. Corporate Flight Bookings LeetCode Solution with the best time and space complexity. The solution to Corporate Flight Bookings problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Corporate Flight Bookings solution in C++
  4. Corporate Flight Bookings solution in Java
  5. Corporate Flight Bookings solution in Python
  6. Additional Resources
1109. Corporate Flight Bookings LeetCode Solution image

Problem Statement of 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

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

1109. Corporate Flight Bookings LeetCode Solution in C++

class Solution {
 public:
  vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
    vector<int> ans(n);

    for (vector<int>& booking : bookings) {
      ans[booking[0] - 1] += booking[2];
      if (booking[1] < n)
        ans[booking[1]] -= booking[2];
    }

    for (int i = 1; i < n; ++i)
      ans[i] += ans[i - 1];

    return ans;
  }
};
/* code provided by PROGIEZ */

1109. Corporate Flight Bookings LeetCode Solution in Java

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

    for (int[] booking : bookings) {
      ans[booking[0] - 1] += booking[2];
      if (booking[1] < n)
        ans[booking[1]] -= booking[2];
    }

    for (int i = 1; i < n; ++i)
      ans[i] += ans[i - 1];

    return ans;
  }
}
// code provided by PROGIEZ

1109. Corporate Flight Bookings LeetCode Solution in Python

class Solution:
  def corpFlightBookings(self, bookings: list[list[int]], n: int) -> list[int]:
    ans = [0] * n

    for booking in bookings:
      ans[booking[0] - 1] += booking[2]
      if booking[1] < n:
        ans[booking[1]] -= booking[2]

    for i in range(1, n):
      ans[i] += ans[i - 1]

    return ans
# code by PROGIEZ

Additional Resources

See also  602. Friend Requests II: Who Has the Most Friends LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.