-
Notifications
You must be signed in to change notification settings - Fork 0
/
1109.航班预订统计.go
83 lines (77 loc) · 1.8 KB
/
1109.航班预订统计.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* @lc app=leetcode.cn id=1109 lang=golang
*
* [1109] 航班预订统计
*
* https://leetcode-cn.com/problems/corporate-flight-bookings/description/
*
* algorithms
* Medium (60.44%)
* Likes: 341
* Dislikes: 0
* Total Accepted: 77.8K
* Total Submissions: 128.4K
* Testcase Example: '[[1,2,10],[2,3,20],[2,5,25]]\n5'
*
* 这里有 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 * 10^4
* 1 <= bookings.length <= 2 * 10^4
* bookings[i].length == 3
* 1 <= firsti <= lasti <= n
* 1 <= seatsi <= 10^4
*
*
*/
// @lc code=start
func corpFlightBookings(bookings [][]int, n int) []int {
delta := make([]int, n+2)
for _, v := range bookings {
delta[v[0]] += v[2]
delta[v[1]+1] -= v[2]
}
for i := 1; i <= n; i++ {
delta[i] = delta[i-1] + delta[i]
}
return delta[1 : n+1]
}
// @lc code=end