Count of seats booked on each of the given N flights - GeeksforGeeks

23 Jul, 2021
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 25Explanation: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.
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++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to find the total of the// seats booked in each of the flightsvoid corpFlightBookings(    vector<vector<int> >& Bookings,    int N){    // Stores the resultant sequence    vector<int> res(N, 0);     // Traverse the array    for (int i = 0;         i < Bookings.size(); i++) {         // Store the first booked flight        int l = Bookings[i][0];         // Store the last booked flight        int r = Bookings[i][1];         // Store the total number of        // seats booked in flights [l, r]        int K = Bookings[i][2];         // Add K to the flight L        res[l - 1] = res[l - 1] + K;         // Subtract K from flight        // number R + 1        if (r <= res.size() - 1)            res[r] = (-K) + res[r];    }     // Find the prefix sum of the array    for (int i = 1; i < res.size(); i++)        res[i] = res[i] + res[i - 1];     // Print the total number of seats    // booked in each flight    for (int i = 0; i < res.size(); i++) {        cout << res[i] << " ";    }} // Driver Codeint main(){    // Given list of bookings    vector<vector<int> > bookings{ { 1, 3, 100 },                                   { 2, 6, 100 },                                   { 3, 4, 100 } };    int N = 6;     // Function Call    corpFlightBookings(bookings, N);     return 0;}
// Java program for the above approachimport java.util.*; class GFG{ // Function to find the total of the// seats booked in each of the flightsstatic void corpFlightBookings(int [][]Bookings,                               int N){       // Stores the resultant sequence    int res[] = new int[N];     // Traverse the array    for (int i = 0;         i < Bookings.length; i++)    {         // Store the first booked flight        int l = Bookings[i][0];         // Store the last booked flight        int r = Bookings[i][1];         // Store the total number of        // seats booked in flights [l, r]        int K = Bookings[i][2];         // Add K to the flight L        res[l - 1] = res[l - 1] + K;         // Subtract K from flight        // number R + 1        if (r <= res.length - 1)            res[r] = (-K) + res[r];    }     // Find the prefix sum of the array    for (int i = 1; i < res.length; i++)        res[i] = res[i] + res[i - 1];     // Print the total number of seats    // booked in each flight    for (int i = 0; i < res.length; i++)    {        System.out.print(res[i] + " ");    }} // Driver Codepublic static void main(String[] args){       // Given list of bookings    int bookings[][] = { { 1, 3, 100 },                        { 2, 6, 100 },                        { 3, 4, 100 } };    int N = 6;     // Function Call    corpFlightBookings(bookings, N);}} // This code is contributed by 29AjayKumar
# Python3 program for the above approach # Function to find the total of the# seats booked in each of the flightsdef corpFlightBookings(Bookings, N):       # Stores the resultant sequence    res = [0] * N     # Traverse the array    for i in range(len(Bookings)):         # Store the first booked flight        l = Bookings[i][0]         # Store the last booked flight        r = Bookings[i][1]         # Store the total number of        # seats booked in flights [l, r]        K = Bookings[i][2]         # Add K to the flight L        res[l - 1] = res[l - 1] + K         # Subtract K from flight        # number R + 1        if (r <= len(res) - 1):            res[r] = (-K) + res[r]     # Find the prefix sum of the array    for i in range(1, len(res)):        res[i] = res[i] + res[i - 1]     # Print total number of seats    # booked in each flight    for i in range(len(res)):        print(res[i], end = " ") # Driver Codeif __name__ == '__main__':       # Given list of bookings    bookings = [ [ 1, 3, 100 ],               [ 2, 6, 100 ],               [ 3, 4, 100 ] ]    N = 6     # Function Call    corpFlightBookings(bookings, N) # This code is contributed by mohit kumar 29
// C# program for the above approachusing System;class GFG{ // Function to find the total of the// seats booked in each of the flightsstatic void corpFlightBookings(int [,]Bookings,                               int N){       // Stores the resultant sequence    int []res = new int[N];     // Traverse the array    for (int i = 0;         i < Bookings.GetLength(0); i++)    {         // Store the first booked flight        int l = Bookings[i,0];         // Store the last booked flight        int r = Bookings[i,1];         // Store the total number of        // seats booked in flights [l, r]        int K = Bookings[i,2];         // Add K to the flight L        res[l - 1] = res[l - 1] + K;         // Subtract K from flight        // number R + 1        if (r <= res.Length - 1)            res[r] = (-K) + res[r];    }     // Find the prefix sum of the array    for (int i = 1; i < res.Length; i++)        res[i] = res[i] + res[i - 1];     // Print the total number of seats    // booked in each flight    for (int i = 0; i < res.Length; i++)    {        Console.Write(res[i] + " ");    }} // Driver Codepublic static void Main(String[] args){       // Given list of bookings    int [,]bookings = { { 1, 3, 100 },                        { 2, 6, 100 },                        { 3, 4, 100 } };    int N = 6;     // Function Call    corpFlightBookings(bookings, N);}} // This code is contributed by shikhasingrajput
<script> // Javascript program for the above approach // Function to find the total of the// seats booked in each of the flightsfunction corpFlightBookings(Bookings, N){         // Stores the resultant sequence    let res = new Array(N).fill(0);     // Traverse the array    for(let i = 0;            i < Bookings.length; i++)    {                 // Store the first booked flight        let l = Bookings[i][0];         // Store the last booked flight        let r = Bookings[i][1];         // Store the total number of        // seats booked in flights [l, r]        let K = Bookings[i][2];         // Add K to the flight L        res[l - 1] = res[l - 1] + K;         // Subtract K from flight        // number R + 1        if (r <= res.length - 1)            res[r] = (-K) + res[r];    }     // Find the prefix sum of the array    for(let i = 1; i < res.length; i++)        res[i] = res[i] + res[i - 1];     // Print the total number of seats    // booked in each flight    for(let i = 0; i < res.length; i++)    {        document.write(res[i] + " ");    }} // Driver Code // Given list of bookingslet bookings = [ [ 1, 3, 100 ],                 [ 2, 6, 100 ],                 [ 3, 4, 100 ] ];let N = 6; // Function CallcorpFlightBookings(bookings, N); // This code is contributed by splevel62 </script>
100 200 300 200 100 100
 
Time Complexity: O(N)Auxiliary Space: O(N)
mohit kumar 29
29AjayKumar
shikhasingrajput
splevel62
bunnyram19
interview-preparation
prefix-sum
Arrays
Greedy
Mathematical
prefix-sum
Arrays
Greedy
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Multidimensional Arrays in Java
Introduction to Arrays
Linear Search
Python | Using 2D arrays/lists the right way
Dijkstra's shortest path algorithm | Greedy Algo-7
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Huffman Coding | Greedy Algo-3
Write a program to print all permutations of a given string