leetcode-1109-差分

时间:2023-04-07 18:03:25



class Solution {
    public int[] corpFlightBookings(int[][] bookings, int n) {
        int[] diff = new int[n];
        for (int[] booking : bookings) {
            int first = booking[0], last = booking[1], seats = booking[2];
            diff[first-1] += seats;
            if (last < n) {
                diff[last] -= seats;
            }
        }
        
        int[] passengers = new int[n];
        passengers[0] = diff[0];
        for (int i = 1; i < n; i++) {
            passengers[i] = passengers[i-1] + diff[i];
        }
        
        return passengers;
    }
}