当前位置:网站首页>[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array
[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array
2022-07-26 10:40:00 【LittleSeedling】
1109. Flight booking Statistics
The title comes from leetcode, Solutions and ideas represent only personal views . Portal .
difficulty : secondary
Time :-
TAG: Difference array
subject
Here you are n A flight , They are from 1 To n Number .
There is a flight reservation form bookings , No i Booking records bookings[i] = [firsti, lasti, seatsi] Means from firsti To lasti ( contain firsti and lasti ) Of Every flight I made a reservation on seatsi A seat .
Please return a length of n Array of answer, among answer[i] It's a flight i The total number of seats booked on .
Example 1:
Input :bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
Output :[10,55,45,25,25]
explain :
Flight number 1 2 3 4 5
Booking records 1 : 10 10
Booking records 2 : 20 20
Booking records 3 : 25 25 25 25
Total number of seats : 10 55 45 25 25
therefore ,answer = [10,55,45,25,25]
Example 2:
Input :bookings = [[1,2,10],[2,2,15]], n = 2
Output :[10,25]
explain :
Flight number 1 2
Booking records 1 : 10 10
Booking records 2 : 15
Total number of seats : 10 25
therefore ,answer = [10,25]
Tips :
1 <= n <= 2 * 104
1 <= bookings.length <= 2 * 104
bookings[i].length== 3
1 <= firsti <= lasti <= n
1 <= seatsi <= 104
Ideas
If you generate a two-dimensional array to save bookings, In the accumulation . Then it will definitely time out .
We need a way , Maintain a section , Make in A section Modification of interval , Make it easy . And it can get , Section Some The value of the location .
This method is Difference array .
Difference array
Definition of differential array ?
For arrays [1,2,2,4], The difference score group is [1,1,0,2], The second... Of the difference array i i i The number is the... Of the original array i − 1 i-1 i−1 Elements and number i i i The difference between elements .
Characteristics of differential array ?
- Find The prefix and You can get the original array .( Prefixes and can Modify in place Original array ).
- When we want to set an interval of the original array [ l , r ] [l,r] [l,r] Apply an increment i n c inc inc when , Difference array d d d The corresponding change is : d [ l ] d[l] d[l] increase i n c inc inc, d [ r + 1 ] d[r+1] d[r+1] Reduce i n c inc inc. In this way, the modification of the interval becomes the modification of two positions .
Code
class Solution {
public:
vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
// Interval query
vector<int> ans(n+1);
for(auto &booking:bookings){
int first = booking[0];
int last = booking[1];
int seats = booking[2];
ans[first-1] += seats;
ans[last] -= seats;
}
// Calculate the prefix and
for(int i=1;i<n;i++){
ans[i] += ans[i-1];
}
ans.pop_back();
return ans;
}
};
Algorithm complexity
Time complexity : O(n+m).n Is the number of flights ,m by bookings The length of .
Spatial complexity : O(n).n Is a differential array ans length .

边栏推荐
- Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
- 在神州IV开发板上为STemWin 5.22加入触屏驱动
- 剑指Offer(九):变态跳台阶
- 干货likeshop外卖点餐系统开源啦100%开源无加密
- Centos8 (liunx) deploying WTM (asp.net 5) using PgSQL
- Analysis of the transaction problem of chained method call
- 第7期:内卷和躺平,你怎么选
- Analyze the hybrid construction objects in JS in detail (construction plus attributes, prototype plus methods)
- 【机器学习小记】【人脸识别】deeplearning.ai course4 4th week programming
- 剑指Offer(二十):包含min函数的栈
猜你喜欢

第7期:内卷和躺平,你怎么选

Okaleido生态核心权益OKA,尽在聚变Mining模式

Uniapp uses the simple method signalr (only for web debugging, cannot package apps)

mysql20210906

第5期:大学生入职必备技能之二

.NET 开源框架在工业生产中的应用

文案秘籍七步曲至----文献团队协作管理

STM32 Alibaba cloud mqtt esp8266 at command

.net5wtm (asp.net core) PgSQL unpacking operation

Zongzi battle - guess who can win
随机推荐
Okaleido ecological core equity Oka, all in fusion mining mode
kali 查看ip地址
Issue 7: how do you choose between curling up and lying flat
RT-Thread 学习笔记(三)---用SCons 构建编译环境
RT-Thread 学习笔记(一)---配置RT-Thread开发环境
Datav beautiful data screen production experience
多目标优化系列1---NSGA2的非支配排序函数的讲解
.NET操作Redis Hash对象
STM32 Alibaba cloud mqtt esp8266 at command
第6期:大学生应该选择哪种主流编程语言
hx711 数据波动大的问题
点击el-dropdown-item/@click.native
比较器(Comparable与Comparator接口)
uniapp使用简单方法signalR(仅用于web调试,无法打包app)
.NET操作Redis sorted set有序集合
RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)
[leetcode每日一题2021/2/14]765. 情侣牵手
Analyze the hybrid construction objects in JS in detail (construction plus attributes, prototype plus methods)
工厂模式详解
20210807#1 C语言程序结构