leetcode题目:https://leetcode-cn.com/problems/corporate-flight-bookings/submissions/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* corpFlightBookings(int** bookings, int bookingsSize, int* bookingsColSize, int n, int* returnSize){
int *returnArr = (int *)malloc(sizeof(int) * n);
int i, j, k;
for (i = 0; i < n; i++) {
returnArr[i] = 0;
}
// 第 i 站上来了 k 个人, 一直到 第 j 站都在飞机上,到第 j + 1 就不在飞机上了
// 用前缀优化,1,2,10,那么3设成-10,ret[i] += ret[i-1];
/*
1 2 3 4 5
10 -10 (由于后面的4,5都是基于3,相当于都-10了)
20 -20
25
10 10+20+25 55-10 45-20 25
*/
int tmp_i, tmp_j;
for (j = 0; j < bookingsSize; j++) {
tmp_i = bookings[j][0];
tmp_j = bookings[j][1];
returnArr[tmp_i - 1] += bookings[j][2];
if (tmp_j < n) {
returnArr[tmp_j] -= bookings[j][2];
}
}
for (i = 1; i < n; i++) {
returnArr[i] += returnArr[i - 1];
}
*returnSize = n;
return returnArr;
}
原文:https://www.cnblogs.com/ginswonderland/p/14063304.html