1109. Corporate Flight Bookings (差分

same as 370, just this one is 1-index base

class Solution {
    public int[] corpFlightBookings(int[][] bookings, int n) {
        int[] diff = new int[n];
        for (int[] booking : bookings) {
            int start = booking[0];
            int end = booking[1];
            int seats = booking[2];
            diff[start-1] += seats; // compare to 370, this question is 1-index based, so start-1, use end
            if (end < n) {
                diff[end] -= seats;
            }
        }
        int[] result = new int[n];
        result[0] = diff[0];
        for (int i = 1; i < n; i++) {
            result[i] = result[i-1] + diff[i];
        }
        return result;
    }
}

Last updated