You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.0 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package class_2022_08_3_week;
// 这里有 n 个航班它们分别从 1 到 n 进行编号。
// 有一份航班预订表 bookings
// 表中第 i 条预订记录 bookings[i] = [firsti, lasti, seatsi]
// 意味着在从 firsti 到 lasti
//(包含 firsti 和 lasti )的 每个航班 上预订了 seatsi 个座位。
// 请你返回一个长度为 n 的数组 answer里面的元素是每个航班预定的座位总数。
// 测试链接 : https://leetcode.cn/problems/corporate-flight-bookings/
public class Code04_CorporateFlightBookings {
public static int[] corpFlightBookings(int[][] bookings, int n) {
// 1 2 3 4 n
// 0 1 2 3 .. n n+1
int[] cnt = new int[n + 2];
for (int[] book : bookings) {
// start book[0]
// end book[1]
// 票 book[2]
cnt[book[0]] += book[2];
cnt[book[1] + 1] -= book[2];
}
for (int i = 1; i < cnt.length; i++) {
cnt[i] += cnt[i - 1];
}
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
ans[i] = cnt[i + 1];
}
return ans;
}
}