📌  相关文章
📜  给定的N个航班中的每个航班预订的座位数

📅  最后修改于: 2021-05-17 05:55:47             🧑  作者: Mango

给定一个整数N表示航班数和数组预订[] [3] ,每行{a,b,K}的形式表示从航班a到航班b预订了K个座位(考虑基于1的索引) ,任务是查找N个航班中预订的座位数的顺序。

例子:

天真的方法:按照以下步骤以最简单的方法解决问题:

  • 初始化大小为N的数组seq []以存储每个航班中已分配座位的数量。
  • 遍历数组预订[]
  • 对于每个查询{a,b,K} ,将数组seq []中的元素K从索引a添加b
  • 完成上述步骤后,打印数组seq []作为结果。

时间复杂度: O(N 2 )
辅助空间: O(N)

高效方法:为了优化上述方法,其思想是使用“前缀和”的概念。请按照以下步骤解决问题:

  • 初始化大小为(N + 2)的数组seq []以存储所有分配的席位。
  • 遍历数组预订[]
  • 对于每个查询{a,b,K} ,将索引(a – 1)处的数组seq []K,并将索引(b +1)处的元素减K。
  • 对于结果序列,找到数组seq []的前缀和。
  • 完成上述步骤后,打印数组seq []作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the total of the
// seats booked in each of the flights
void corpFlightBookings(
    vector >& Bookings,
    int N)
{
    // Stores the resultant sequence
    vector 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 Code
int main()
{
    // Given list of bookings
    vector > bookings{ { 1, 3, 100 },
                                   { 2, 6, 100 },
                                   { 3, 4, 100 } };
    int N = 6;
 
    // Function Call
    corpFlightBookings(bookings, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
 
// Function to find the total of the
// seats booked in each of the flights
static 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 Code
public 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
# Python3 program for the above approach
 
# Function to find the total of the
# seats booked in each of the flights
def 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]
 
    # Prthe total number of seats
    # booked in each flight
    for i in range(len(res)):
        print(res[i], end = " ")
 
# Driver Code
if __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#
// C# program for the above approach
using System;
class GFG
{
 
// Function to find the total of the
// seats booked in each of the flights
static 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 Code
public 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


输出:
100 200 300 200 100 100

时间复杂度: O(N)
辅助空间: O(N)