Skip to content

Commit 924534b

Browse files
authored
Merge pull request #3847 from sreevidya-16/main
Add Solution to LC problem 1109
2 parents 95ef7a9 + 98267a0 commit 924534b

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
---
2+
id: corporate-flight-bookings
3+
title: Corporate Flight Bookings
4+
sidebar_label: Corporate Flight Bookings
5+
tags: [Array, Prefix Sum, C++, Python, Java]
6+
description: Solve the problem of calculating the total number of seats reserved for each flight using the booking intervals and prefix sum technique.
7+
---
8+
9+
## Problem Statement
10+
11+
### Problem Description
12+
13+
There are `n` flights that are labeled from 1 to `n`.
14+
15+
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.
16+
17+
Return an array `answer` of length `n`, where `answer[i]` is the total number of seats reserved for flight `i`.
18+
19+
### Example
20+
21+
**Example 1:**
22+
23+
```
24+
Input: bookings = [[1, 2, 10], [2, 3, 20], [2, 5, 25]], n = 5
25+
Output: [10, 55, 45, 25, 25]
26+
```
27+
**Explanation:**
28+
Flight labels: 1 2 3 4 5
29+
Booking 1 reserved: 10 10
30+
Booking 2 reserved: 20 20
31+
Booking 3 reserved: 25 25 25 25
32+
Total seats: 10 55 45 25 25
33+
Hence, answer = [10, 55, 45, 25, 25]
34+
35+
**Example 2:**
36+
```
37+
Input: bookings = [[1, 2, 10], [2, 2, 15]], n = 2
38+
Output: [10, 25]
39+
```
40+
**Explanation:**
41+
Flight labels: 1 2
42+
Booking 1 reserved: 10 10
43+
Booking 2 reserved: 15
44+
Total seats: 10 25
45+
Hence, answer = [10, 25]
46+
47+
48+
### Constraints
49+
50+
- 1 <= `n` <= 2 * 10^4
51+
- 1 <= `bookings.length` <= 2 * 10^4
52+
- `bookings[i].length` == 3
53+
- 1 <= `firsti` <= `lasti` <= `n`
54+
- 1 <= `seatsi` <= 10^4
55+
56+
## Solution
57+
58+
### Intuition
59+
60+
To solve this problem efficiently, we use a difference array (also known as a prefix sum array). The idea is to use an auxiliary array to keep track of seat reservations in a way that allows us to compute the final seat count for each flight in a single pass.
61+
62+
1. **Use a Difference Array**:
63+
- Create an array `arr` of size `n+1` to track the seat changes.
64+
- For each booking `[firsti, lasti, seatsi]`, increment `arr[firsti-1]` by `seatsi` and decrement `arr[lasti]` by `seatsi` (if `lasti < n`).
65+
66+
2. **Compute the Prefix Sum**:
67+
- Traverse the difference array to compute the prefix sum which gives the total number of seats reserved for each flight.
68+
69+
### Time Complexity and Space Complexity Analysis
70+
71+
- **Time Complexity**:
72+
- The time complexity is $O(m + n)$, where `m` is the number of bookings and `n` is the number of flights. This is due to the single pass required for processing the bookings and computing the prefix sum.
73+
74+
- **Space Complexity**:
75+
- The space complexity is $O(n)$, which is required to store the difference array and the final answer array.
76+
77+
### Code
78+
79+
#### C++
80+
81+
```cpp
82+
class Solution {
83+
public:
84+
vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
85+
vector<int> arr(n + 1, 0);
86+
for (const auto& booking : bookings) {
87+
int start = booking[0] - 1;
88+
int end = booking[1];
89+
int seats = booking[2];
90+
arr[start] += seats;
91+
if (end &lt; n) {
92+
arr[end] -= seats;
93+
}
94+
}
95+
96+
vector<int> answer(n);
97+
answer[0] = arr[0];
98+
for (int i = 1; i &lt; n; ++i) {
99+
answer[i] = answer[i - 1] + arr[i];
100+
}
101+
return answer;
102+
}
103+
};
104+
```
105+
#### Python
106+
```python
107+
class Solution:
108+
def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:
109+
arr = [0] * (n + 1)
110+
for start, end, seats in bookings:
111+
arr[start - 1] += seats
112+
if end &lt; n:
113+
arr[end] -= seats
114+
115+
answer = [0] * n
116+
answer[0] = arr[0]
117+
for i in range(1, n):
118+
answer[i] = answer[i - 1] + arr[i]
119+
120+
return answer
121+
```
122+
#### Java
123+
```java
124+
class Solution {
125+
public int[] corpFlightBookings(int[][] bookings, int n) {
126+
int[] arr = new int[n + 1];
127+
for (int[] booking : bookings) {
128+
int start = booking[0] - 1;
129+
int end = booking[1];
130+
int seats = booking[2];
131+
arr[start] += seats;
132+
if (end &lt; n) {
133+
arr[end] -= seats;
134+
}
135+
}
136+
137+
int[] answer = new int[n];
138+
answer[0] = arr[0];
139+
for (int i = 1; i &lt; n; i++) {
140+
answer[i] = answer[i - 1] + arr[i];
141+
}
142+
143+
return answer;
144+
}
145+
}
146+
```

0 commit comments

Comments
 (0)