-
Notifications
You must be signed in to change notification settings - Fork 0
/
corporate-flight-bookings.py
34 lines (29 loc) · 1.09 KB
/
corporate-flight-bookings.py
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
# 这题和 range-addtion 一模一样,这两题都是需要注意一下python的面向对象的编程
# https://leetcode.cn/problems/corporate-flight-bookings/
class difference:
def __init__(self, nums: List[int]):
if len(nums) > 0:
self.diff = [0 for _ in range(len(nums))]
self.diff[0] = nums[0]
for i in range(1, len(nums)):
self.diff[i] = nums[i]-nums[i-1]
def increment(self, i, j, val):
self.diff[i] += val
if j+1 < len(self.diff):
self.diff[j+1] -= val
def result(self):
res = [0 for _ in range(len(self.diff))]
res[0] = self.diff[0]
for i in range(1, len(self.diff)):
res[i] = res[i-1]+self.diff[i]
return res
class Solution:
def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:
nums = [0 for _ in range(n)]
df = difference(nums)
for booking in bookings:
x = booking[0]-1
y = booking[1]-1
val = booking[2]
df.increment(x, y, val)
return df.result()