Skip to content

Instantly share code, notes, and snippets.

@alxfv
Created July 9, 2019 14:49
Show Gist options
  • Save alxfv/ac09c465d5a46757e044ce29cec726e8 to your computer and use it in GitHub Desktop.
Save alxfv/ac09c465d5a46757e044ce29cec726e8 to your computer and use it in GitHub Desktop.
class Solution:
def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:
prefix_sum = [0] * (n + 1)
# 1. Build prefix sum array
# for booking [1, 3, 5] and n = 5 prefix sum array:
# [0, 0, 0, 0, 0] -> [5, 0, 0, -5, 0, 0]
for lo, hi, seats in bookings:
prefix_sum[lo - 1] += seats
prefix_sum[hi] -= seats
# 2. Build result array from prefix sum array
# [5, 0, 0, -5, 0, 0] -> [5, 5, 5, 0, 0]
ret = [0] * n
ret[0] = prefix_sum[0]
for i in range(1, n):
ret[i] = ret[i - 1] + prefix_sum[i]
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment