乍一看这不很简单吗,循环first和last相加不就可以了吗,两分钟搞出来:
public int[] corpFlightBookings(int[][] bookings, int n) {
int[] bookSeats = new int[n] ;
for(int[] book:bookings){
for(int j = book[0]-1 ;j < book[1] ; j ++){
bookSeats[j] += book[2] ;
}
}
return bookSeats ;
}
结果很残酷:
看评论有一个大神,利用差分数组执行竟然只需要2ms ,过程如下
加入有5个航班,先声明一个差分数组:res[5] ,有个预定为[2 ,4 , 40]
那么rest[1] - res[3] 都要加40 ,只有res[0] 和res[4]不用加,
那么我们就假设从下标1 开始后面全部加40 ,那么只需要差分数组res[1] + 10 即可,但是res[4]是不加的,并且如果后面还有后面也是不加的