给定一个表示航班数量的整数N和数组bookings[][3] ,每行的形式为{a, b, K}表示从航班a到航班b预订了K个座位(考虑基于 1 的索引) ,任务是找到N个航班中预订的座位数的顺序。
例子:
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
朴素的方法:按照以下步骤以最简单的方法解决问题:
- 初始化大小为N的数组seq[]以存储每个航班中分配的座位数。
- 遍历数组bookings[] 。
- 对于每个查询{a, b, K} ,添加数组seq[] 中从索引a到b的元素K。
- 完成上述步骤后,打印数组seq[]作为结果。
时间复杂度: O(N 2 )
辅助空间: O(N)
高效方法:为了优化上述方法,思想是使用Prefix Sum的概念。请按照以下步骤解决问题:
- 初始化一个大小为(N + 2)的数组seq[]来存储所有分配的座位。
- 遍历数组bookings[] 。
- 对于每个查询{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]
# Print 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
Javascript
100 200 300 200 100 100
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。