Skip to content

Latest commit

 

History

History
25 lines (21 loc) · 739 Bytes

1109.航班预订统计.md

File metadata and controls

25 lines (21 loc) · 739 Bytes

1109.航班预订统计

https://leetcode-cn.com/problems/corporate-flight-bookings/

差分数组

直接套用差分模板求出最终数组,注意航班编号从1开始,而模板从0开始

class Solution:
    def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:
        diff = [0] * n
        for booking in bookings:
            i = booking[0]-1
            j = booking[1]-1
            val = booking[2]
            diff[i] += val
            if j+1 < n:
                diff[j+1] -= val
        res = [0] * n
        res[0] = diff[0]
        for i in range(1, n):
            res[i] = res[i-1] + diff[i]
        return res