-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm1109.java
More file actions
29 lines (29 loc) · 1.07 KB
/
prblm1109.java
File metadata and controls
29 lines (29 loc) · 1.07 KB
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
public class prblm1109 {
public static void main(String[] args) {
int[][] bookings = {{1,2,10}, {2,3,20}, {2,5,25}};
int n = 5;
int[] answer = corpFlightBookings(bookings, n);
for (int i = 0; i < answer.length; i++) {
System.out.println(answer[i]);
}
}
static int[] corpFlightBookings(int[][] bookings, int n) {
int[] prefixSum = new int[n+2];
for (int i=0; i< bookings.length; i++) {
int[] booking = bookings[i];
int left = booking[0];
int right = booking[1];
prefixSum[left] += booking[2];
prefixSum[right+1] -= booking[2];
}
return calculatePrefixSum(prefixSum);
}
static int[] calculatePrefixSum(int[] prefixSum){
int[] calculatedPrefixSum = new int[prefixSum.length-2];
calculatedPrefixSum[0] = prefixSum[1];
for (int i = 1; i < calculatedPrefixSum.length; i++) {
calculatedPrefixSum[i] = calculatedPrefixSum[i-1] + prefixSum[i+1];
}
return calculatedPrefixSum;
}
}