当前位置:网站首页>Simulation volume leetcode [general] 1109 Flight reservation statistics

Simulation volume leetcode [general] 1109 Flight reservation statistics

2022-07-06 06:17:00 Encounter simulation volume

1109. Flight booking Statistics

Here you are  n  A flight , They are from 1 To n Number .

There is a flight reservation form  bookings , No  i  Booking records  bookings[i] = [firsti, lasti, seatsi]  Means from firsti  To lasti ( contain firsti and lasti ) Of Every flight I made a reservation on seatsi  A seat .

Please return a length of n Array of  answer, among answer[i] It's a flight i The total number of seats booked on .

Example 1:

Input :bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
Output :[10,55,45,25,25]
explain :
Flight number 1 2 3 4 5
Booking records 1 : 10 10
Booking records 2 : 20 20
Booking records 3 : 25 25 25 25
Total number of seats : 10 55 45 25 25
therefore ,answer = [10,55,45,25,25]
Example 2:

Input :bookings = [[1,2,10],[2,2,15]], n = 2
Output :[10,25]
explain :
Flight number 1 2
Booking records 1 : 10 10
Booking records 2 : 15
Total number of seats : 10 25
therefore ,answer = [10,25]

Tips :

1 <= n <= 2 * 104
1 <= bookings.length <= 2 * 104
bookings[i].length == 3
1 <= firsti <= lasti <= n
1 <= seatsi <= 104

source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/corporate-flight-bookings
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .

Code :

import time
from typing import List


class Solution:
    def __init__(self):
        pass

    def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:
        bookings_save = [0 for _ in range(n+2)]
        for first,last,seats in bookings:
            bookings_save[first]+=seats
            bookings_save[last+1]-=seats
        result = [bookings_save[1]]
        for x in bookings_save[2:-1]:
            result.append(x+result[-1])
        return result


def test(data_test):
    s = Solution()
    return s.corpFlightBookings(*data_test)


def test_obj(data_test):
    result = [None]
    obj = Solution(*data_test[1][0])
    for fun, data in zip(data_test[0][1::], data_test[1][1::]):
        if data:
            res = obj.__getattribute__(fun)(*data)
        else:
            res = obj.__getattribute__(fun)()
        result.append(res)
    return result


if __name__ == '__main__':
    datas = [
        [[[1,2,10],[2,3,20],[2,5,25]],5],
        [[[1,2,10],[2,2,15]],2],
        # [],
    ]
    for data_test in datas:
        t0 = time.time()
        print('-' * 50)
        print('input:', data_test)
        print('output:', test(data_test))
        print(f'use time:{
      time.time() - t0}s')

remarks :
GitHub:https://github.com/monijuan/leetcode_python

CSDN Summary : Simulation volume Leetcode Summary of questions _ Paper blog -CSDN Blog

You can add QQ Group communication :1092754609

leetcode_python.utils See the description on the summary page for details
First brush questions , Then generated by script blog, If there is any mistake, please leave a message , I see it will be revised ! thank you !

原网站

版权声明
本文为[Encounter simulation volume]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060615157753.html