#1109
Medium Algorithms Corporate flight bookings
Array Prefix Sum
66.5% acceptance
Feb 25, 2026
1856
166
There are n flights that are labeled from 1 to n.
You are given an array of flight bookings bookings, where bookings[i] = [firsti, lasti, seatsi] represents a booking for flights firsti through lasti (inclusive) with seatsi seats reserved for each flight in the range.
Return an array answer of length n, where answer[i] is the total number of seats reserved for flight i.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn corp_flight_bookings(bookings: Vec<Vec<i32>>, n: i32) -> Vec<i32> {
let n = n as usize;
let mut diff = vec![0i32; n + 1];
for b in &bookings {
let (first, last, seats) = (b[0] as usize - 1, b[1] as usize, b[2]);
diff[first] += seats;
diff[last] -= seats;
}
let mut result = vec![0i32; n];
let mut cur = 0;
for i in 0..n {
cur += diff[i];
result[i] = cur;
}
result
}
}