Count of seats booked on each of the given N flights
Given an integer N denoting the number of flights and array bookings[][3] with each row of the form {a, b, K} denoting that there are K seats booked from flight a to flight b ( consider 1-based indexing), the task is to find the sequence of the number of seats booked in N flights.
Examples:
Input: bookings[][] = {{1, 2, 10}, {2, 3, 20}, {2, 5, 25}}
Output: 10 55 45 25 25
Explanation:
Initially, there are no seats booked in any of the flights. So the resultant sequence is {0, 0, 0, 0, 0}
Book 10 seats in flights 1 to 2. The sequence becomes {10, 10, 0, 0, 0}.
Book 20 seats in flights 2 to 3. The sequence becomes {10, 30, 20, 0, 0}.
Book 25 seats in flights 2 to 5. The sequence becomes {10, 55, 45, 25, 25}.
Input: bookings[][] = {{1, 3, 100}, {2, 6, 100}, {3, 4, 100}}
Output: 100 200 300 200 100 100
Naive Approach: Follow the steps below to solve the problem in simplest approach possible:
- Initialize an array seq[] of size N to store the count of allocated seats in each flight.
- Traverse the array bookings[].
- For each query {a, b, K}, add the element K in the array seq[] from the index a to b.
- After completing the above steps, print the array seq[] as the result.
Below is the code for the above naive approach :
C++
#include <bits/stdc++.h>
using namespace std;
void corpFlightBookings(vector<vector< int > >& Bookings,
int N)
{
vector< int > seq(N, 0);
for ( int i = 0; i < Bookings.size(); i++) {
int l = Bookings[i][0];
int r = Bookings[i][1];
int K = Bookings[i][2];
for ( int j = l - 1; j < r; j++) {
seq[j] += K;
}
}
for ( int i = 0; i < seq.size(); i++) {
cout << seq[i] << " " ;
}
}
int main()
{
vector<vector< int > > bookings{ { 1, 3, 100 },
{ 2, 6, 100 },
{ 3, 4, 100 } };
int N = 6;
corpFlightBookings(bookings, N);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class CorporateFlightBookings {
static void corpFlightBookings(List<List<Integer>> bookings, int N) {
int [] seq = new int [N];
for (List<Integer> booking : bookings) {
int l = booking.get( 0 );
int r = booking.get( 1 );
int K = booking.get( 2 );
for ( int j = l - 1 ; j < r; j++) {
seq[j] += K;
}
}
for ( int i = 0 ; i < seq.length; i++) {
System.out.print(seq[i] + " " );
}
}
public static void main(String[] args) {
List<List<Integer>> bookings = new ArrayList<>();
bookings.add(List.of( 1 , 3 , 100 ));
bookings.add(List.of( 2 , 6 , 100 ));
bookings.add(List.of( 3 , 4 , 100 ));
int N = 6 ;
corpFlightBookings(bookings, N);
}
}
|
Python3
def corpFlightBookings(bookings, N):
seq = [ 0 ] * N
for i in range ( len (bookings)):
l = bookings[i][ 0 ]
r = bookings[i][ 1 ]
K = bookings[i][ 2 ]
for j in range (l - 1 , r):
seq[j] + = K
for i in range ( len (seq)):
print (seq[i], end = " " )
if __name__ = = "__main__" :
bookings = [[ 1 , 3 , 100 ],
[ 2 , 6 , 100 ],
[ 3 , 4 , 100 ]]
N = 6
corpFlightBookings(bookings, N)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static void CorpFlightBookings(List<List< int >> bookings, int N)
{
int [] seq = new int [N];
foreach ( var booking in bookings)
{
int l = booking[0];
int r = booking[1];
int K = booking[2];
for ( int j = l - 1; j < r; j++)
{
seq[j] += K;
}
}
for ( int i = 0; i < seq.Length; i++)
{
Console.Write(seq[i] + " " );
}
}
static void Main()
{
List<List< int >> bookings = new List<List< int >>
{
new List< int > {1, 3, 100},
new List< int > {2, 6, 100},
new List< int > {3, 4, 100}
};
int N = 6;
CorpFlightBookings(bookings, N);
}
}
|
Javascript
function corpFlightBookings(bookings, N) {
const seq = new Array(N).fill(0);
for (const booking of bookings) {
const l = booking[0];
const r = booking[1];
const K = booking[2];
for (let j = l - 1; j < r; j++) {
seq[j] += K;
}
}
console.log(seq.join( ' ' ));
}
const bookings = [
[1, 3, 100],
[2, 6, 100],
[3, 4, 100]
];
const N = 6;
corpFlightBookings(bookings, N);
|
Output
100 200 300 200 100 100
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to use the concept of Prefix Sum. Follow the steps below to solve the problem:
- Initialize an array seq[] of size (N + 2) to store all the allocated seats.
- Traverse the array bookings[].
- For each query {a, b, K}, increment the array seq[] at index (a – 1) by K and decrement the element at index (b + 1) by K.
- For the resulting sequence, find the prefix sum of the array seq[].
- After completing the above steps, print the array seq[] as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void corpFlightBookings(
vector<vector< int > >& Bookings,
int N)
{
vector< int > res(N, 0);
for ( int i = 0;
i < Bookings.size(); i++) {
int l = Bookings[i][0];
int r = Bookings[i][1];
int K = Bookings[i][2];
res[l - 1] = res[l - 1] + K;
if (r <= res.size() - 1)
res[r] = (-K) + res[r];
}
for ( int i = 1; i < res.size(); i++)
res[i] = res[i] + res[i - 1];
for ( int i = 0; i < res.size(); i++) {
cout << res[i] << " " ;
}
}
int main()
{
vector<vector< int > > bookings{ { 1, 3, 100 },
{ 2, 6, 100 },
{ 3, 4, 100 } };
int N = 6;
corpFlightBookings(bookings, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void corpFlightBookings( int [][]Bookings,
int N)
{
int res[] = new int [N];
for ( int i = 0 ;
i < Bookings.length; i++)
{
int l = Bookings[i][ 0 ];
int r = Bookings[i][ 1 ];
int K = Bookings[i][ 2 ];
res[l - 1 ] = res[l - 1 ] + K;
if (r <= res.length - 1 )
res[r] = (-K) + res[r];
}
for ( int i = 1 ; i < res.length; i++)
res[i] = res[i] + res[i - 1 ];
for ( int i = 0 ; i < res.length; i++)
{
System.out.print(res[i] + " " );
}
}
public static void main(String[] args)
{
int bookings[][] = { { 1 , 3 , 100 },
{ 2 , 6 , 100 },
{ 3 , 4 , 100 } };
int N = 6 ;
corpFlightBookings(bookings, N);
}
}
|
Python3
def corpFlightBookings(Bookings, N):
res = [ 0 ] * N
for i in range ( len (Bookings)):
l = Bookings[i][ 0 ]
r = Bookings[i][ 1 ]
K = Bookings[i][ 2 ]
res[l - 1 ] = res[l - 1 ] + K
if (r < = len (res) - 1 ):
res[r] = ( - K) + res[r]
for i in range ( 1 , len (res)):
res[i] = res[i] + res[i - 1 ]
for i in range ( len (res)):
print (res[i], end = " " )
if __name__ = = '__main__' :
bookings = [ [ 1 , 3 , 100 ],
[ 2 , 6 , 100 ],
[ 3 , 4 , 100 ] ]
N = 6
corpFlightBookings(bookings, N)
|
C#
using System;
class GFG
{
static void corpFlightBookings( int [,]Bookings,
int N)
{
int []res = new int [N];
for ( int i = 0;
i < Bookings.GetLength(0); i++)
{
int l = Bookings[i,0];
int r = Bookings[i,1];
int K = Bookings[i,2];
res[l - 1] = res[l - 1] + K;
if (r <= res.Length - 1)
res[r] = (-K) + res[r];
}
for ( int i = 1; i < res.Length; i++)
res[i] = res[i] + res[i - 1];
for ( int i = 0; i < res.Length; i++)
{
Console.Write(res[i] + " " );
}
}
public static void Main(String[] args)
{
int [,]bookings = { { 1, 3, 100 },
{ 2, 6, 100 },
{ 3, 4, 100 } };
int N = 6;
corpFlightBookings(bookings, N);
}
}
|
Javascript
<script>
function corpFlightBookings(Bookings, N)
{
let res = new Array(N).fill(0);
for (let i = 0;
i < Bookings.length; i++)
{
let l = Bookings[i][0];
let r = Bookings[i][1];
let K = Bookings[i][2];
res[l - 1] = res[l - 1] + K;
if (r <= res.length - 1)
res[r] = (-K) + res[r];
}
for (let i = 1; i < res.length; i++)
res[i] = res[i] + res[i - 1];
for (let i = 0; i < res.length; i++)
{
document.write(res[i] + " " );
}
}
let bookings = [ [ 1, 3, 100 ],
[ 2, 6, 100 ],
[ 3, 4, 100 ] ];
let N = 6;
corpFlightBookings(bookings, N);
</script>
|
Output
100 200 300 200 100 100
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
04 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...