个人网站建设教学视频百度云盘,在网站接入银联怎么做,软件开发外包工作室,荟萃浏览器文件夹这里有 n 个航班#xff0c;它们分别从 1 到 n 进行编号。
有一份航班预订表 bookings #xff0c;表中第 i 条预订记录 bookings[i] [firsti, lasti, seatsi] 意味着在从 firsti 到 lasti #xff08;包含 firsti 和 lasti #xff09;的 每个航班 上预订了 seatsi 个座…这里有 n 个航班它们分别从 1 到 n 进行编号。
有一份航班预订表 bookings 表中第 i 条预订记录 bookings[i] [firsti, lasti, seatsi] 意味着在从 firsti 到 lasti 包含 firsti 和 lasti 的 每个航班 上预订了 seatsi 个座位。
请你返回一个长度为 n 的数组 answer里面的元素是每个航班预定的座位总数。
示例 1
输入bookings [[1,2,10],[2,3,20],[2,5,25]], n 5
输出[10,55,45,25,25]
解释
航班编号 1 2 3 4 5
预订记录 1 10 10
预订记录 2 20 20
预订记录 3 25 25 25 25
总座位数 10 55 45 25 25
因此answer [10,55,45,25,25]示例 2
输入bookings [[1,2,10],[2,2,15]], n 2
输出[10,25]
解释
航班编号 1 2
预订记录 1 10 10
预订记录 2 15
总座位数 10 25
因此answer [10,25]提示
1 n 2 * 1041 bookings.length 2 * 104bookings[i].length 31 firsti lasti n1 seatsi 104
思路
差分数组经典题目。
这里就不介绍差分数组了简单理解就有点类似多米诺骨牌只需要在开始的位置改变数据减加在结束1的位置改变数据加减。
class Solution {public int[] corpFlightBookings(int[][] bookings, int n) {int[] cha new int[n1];for (int i0;ibookings.length;i) {cha[bookings[i][0]-1] bookings[i][2];cha[bookings[i][1]] - bookings[i][2];}int[] ans new int[n];for (int i0;in;i) {if (i0) ans[i] cha[i];else {ans[i] cha[i] ans[i-1];}}return ans;}
}