Skip to content

Commit b9799db

Browse files
committed
add 1109 java
1 parent 85def7f commit b9799db

File tree

5 files changed

+234
-25
lines changed

5 files changed

+234
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@
317317
1014 | [Best Sightseeing Pair](https://leetcode.com/problems/best-sightseeing-pair/) | [Python](./leetcode_python/Array/best_sightseeing_pair.py) | | | Medium |dp, array, good basic, Spotify| AGAIN (not start)
318318
1027 | [Longest Arithmetic Subsequence](https://leetcode.com/problems/longest-arithmetic-subsequence/) | [Python](./leetcode_python/Array/longest-arithmetic-subsequence.py) | | | Medium |dp, hash table, trick, google, amazon| AGAIN*** (not start)
319319
1041 | [Robot Bounded In Circle](https://leetcode.com/problems/robot-bounded-in-circle/) | [Python](./leetcode_python/Array/robot-bounded-in-circle.py) | | | Medium |array, math, `amazon`| AGAIN** (2)
320-
1109 | [ Corporate Flight Bookings](https://leetcode.com/problems/corporate-flight-bookings/) | [Python](./leetcode_python/Array/corporate-flight-bookings.py) | | | Medium |array, `amazon`|AGAIN (not start)
320+
1109 | [ Corporate Flight Bookings](https://leetcode.com/problems/corporate-flight-bookings/) | [Python](./leetcode_python/Array/corporate-flight-bookings.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/CorporateFlightBookings.java) | | |Medium |difference array, basic, `amazon`, google|AGAIN**** (2)
321321
1248 | [Count Number of Nice Subarrays](https://leetcode.com/problems/count-number-of-nice-subarrays/) | [Python](./leetcode_python/Array/count-number-of-nice-subarrays.py) | | | Medium |LC 828, hash map, Prefix sum, dict, windown, good basic, deque, array, `amazon`| AGAIN************** (3) (MUST)
322322
1275 | [Find Winner on a Tic Tac Toe Game](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/) | [Python](./leetcode_python/Array/find-winner-on-a-tic-tac-toe-game.py) | | | Easy |`amazon`| AGAIN (not start)
323323
1567 | [Maximum Length of Subarray With Positive Product](https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/) | [Python](./leetcode_python/Array/maximum-length-of-subarray-with-positive-product.py) | | | Medium |good trick, array, dp, 2 pointers, amazon| AGAIN**** (1) (not start)

data/progress.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
20241220: 815,871,593
2-
20241215: 1109
1+
20241220: 815,871,593,1109
32
20241214: 560,523
43
20241208: 304,853,325
54
20241202: 370(todo)

data/to_review.txt

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,36 @@
1-
2025-02-13 -> ['815,871,593']
2-
2025-02-08 -> ['1109']
1+
2025-02-13 -> ['815,871,593,1109']
32
2025-02-07 -> ['560,523']
43
2025-02-01 -> ['304,853,325']
54
2025-01-26 -> ['370(todo)']
65
2025-01-24 -> ['34,767']
7-
2025-01-23 -> ['815,871,593']
6+
2025-01-23 -> ['815,871,593,1109']
87
2025-01-20 -> ['722,380']
98
2025-01-19 -> ['33,81']
10-
2025-01-18 -> ['1109']
119
2025-01-17 -> ['560,523', '253']
1210
2025-01-16 -> ['776,31']
1311
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
1412
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
1513
2025-01-11 -> ['304,853,325', '394']
16-
2025-01-10 -> ['815,871,593', '833,950']
17-
2025-01-05 -> ['1109', '370(todo)']
14+
2025-01-10 -> ['815,871,593,1109', '833,950']
15+
2025-01-05 -> ['370(todo)']
1816
2025-01-04 -> ['560,523', '53,210,207']
1917
2025-01-03 -> ['34,767', '444']
20-
2025-01-02 -> ['815,871,593', '1188,130,855(again)']
18+
2025-01-02 -> ['815,871,593,1109', '1188,130,855(again)']
2119
2024-12-30 -> ['722,380']
2220
2024-12-29 -> ['304,853,325', '33,81']
23-
2024-12-28 -> ['815,871,593', '1109', '900']
21+
2024-12-28 -> ['815,871,593,1109', '900']
2422
2024-12-27 -> ['560,523', '253', '26,27', '802,1197,26']
2523
2024-12-26 -> ['776,31']
26-
2024-12-25 -> ['815,871,593', '004(todo),34(todo),162(todo),275(todo)']
24+
2024-12-25 -> ['815,871,593,1109', '004(todo),34(todo),162(todo),275(todo)']
2725
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
28-
2024-12-23 -> ['815,871,593', '1109', '370(todo)']
29-
2024-12-22 -> ['815,871,593', '560,523']
30-
2024-12-21 -> ['815,871,593', '304,853,325', '34,767', '394', '855,846']
31-
2024-12-20 -> ['1109', '833,950', '932']
26+
2024-12-23 -> ['815,871,593,1109', '370(todo)']
27+
2024-12-22 -> ['815,871,593,1109', '560,523']
28+
2024-12-21 -> ['815,871,593,1109', '304,853,325', '34,767', '394', '855,846']
29+
2024-12-20 -> ['833,950', '932']
3230
2024-12-19 -> ['560,523']
33-
2024-12-18 -> ['1109', '951,792']
34-
2024-12-17 -> ['1109', '560,523', '722,380']
35-
2024-12-16 -> ['1109', '560,523', '304,853,325', '33,81']
31+
2024-12-18 -> ['951,792']
32+
2024-12-17 -> ['560,523', '722,380']
33+
2024-12-16 -> ['560,523', '304,853,325', '33,81']
3634
2024-12-15 -> ['560,523', '370(todo)']
3735
2024-12-14 -> ['253', '53,210,207', '163,1048']
3836
2024-12-13 -> ['304,853,325', '34,767', '776,31', '444', '298,729']

leetcode_java/src/main/java/LeetCodeJava/Array/CorporateFlightBookings.java

Lines changed: 121 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package LeetCodeJava.Array;
22

33
// https://leetcode.com/problems/corporate-flight-bookings/description/
4+
// https://leetcode.cn/problems/corporate-flight-bookings/description/
5+
6+
import java.util.Arrays;
7+
48
/**
59
* 1109. Corporate Flight Bookings
610
* Solved
@@ -46,15 +50,126 @@
4650
* bookings[i].length == 3
4751
* 1 <= firsti <= lasti <= n
4852
* 1 <= seatsi <= 104
53+
*
54+
*
55+
* Example :
56+
*
57+
*
58+
* 示例 1:
59+
*
60+
* 输入:bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
61+
* 输出:[10,55,45,25,25]
62+
* 解释:
63+
* 航班编号 1 2 3 4 5
64+
* 预订记录 1 : 10 10
65+
* 预订记录 2 : 20 20
66+
* 预订记录 3 : 25 25 25 25
67+
* 总座位数: 10 55 45 25 25
68+
* 因此,answer = [10,55,45,25,25]
69+
* 示例 2:
70+
*
71+
* 输入:bookings = [[1,2,10],[2,2,15]], n = 2
72+
* 输出:[10,25]
73+
* 解释:
74+
* 航班编号 1 2
75+
* 预订记录 1 : 10 10
76+
* 预订记录 2 : 15
77+
* 总座位数: 10 25
78+
* 因此,answer = [10,25]
79+
*
4980
*/
5081
public class CorporateFlightBookings {
5182

52-
// V0
53-
// public int[] corpFlightBookings(int[][] bookings, int n) {
54-
//
55-
// }
83+
// V0
84+
// TODO : implement
85+
// public int[] corpFlightBookings(int[][] bookings, int n) {
86+
//
87+
// }
88+
89+
// V1
90+
// IDEA : difference ARRAY (gpt)
91+
public int[] corpFlightBookings_1(int[][] bookings, int n) {
92+
int[] res = new int[n + 1]; // Use n+1 to handle range updates easily with a difference array.
93+
94+
// Create a difference array
95+
for (int[] booking : bookings) {
96+
int start = booking[0]; // Starting index (1-based)
97+
int end = booking[1]; // Ending index (1-based)
98+
int seats = booking[2]; // Seats to add
99+
100+
res[start] += seats; // Add seats at the start
101+
if (end + 1 <= n) { // Subtract seats after the end
102+
res[end + 1] -= seats;
103+
}
104+
}
105+
106+
// Compute the prefix sum to get the final result
107+
for (int i = 1; i <= n; i++) {
108+
res[i] += res[i - 1];
109+
}
110+
111+
// Exclude the extra index used for the difference array
112+
return Arrays.copyOfRange(res, 1, n + 1);
113+
}
114+
115+
// V2
116+
// https://leetcode.com/problems/corporate-flight-bookings/submissions/1483568206/
117+
public int[] corpFlightBookings_2(int[][] bookings, int n) {
118+
int[] ans = new int[n];
119+
for (int[] booking : bookings) {
120+
int i = booking[0] - 1;
121+
int j = booking[1];
122+
int seats = booking[2];
123+
ans[i] += seats;
124+
if (j != n)
125+
ans[j] -= seats;
126+
}
127+
128+
int count = 0;
129+
for (int i = 0; i < ans.length; i++) {
130+
ans[i] += count;
131+
count = ans[i];
132+
}
133+
return ans;
134+
}
135+
136+
// V3-1
137+
// https://leetcode.com/problems/corporate-flight-bookings/solutions/1338804/java-2-approaches-brute-force-optimal-ap-e12t/
138+
// IDEA : BRUTE FORCE
139+
public int[] corpFlightBookings_3_1(int[][] bookings, int n) {
140+
141+
int[] res = new int[n];
142+
for (int[] curr : bookings) {
143+
int start = curr[0] - 1;
144+
int end = curr[1];
145+
int val = curr[2];
146+
147+
for (int i = start; i < end; i++) {
148+
res[i] += val;
149+
}
150+
}
151+
return res;
152+
}
153+
154+
// V3-2
155+
// https://leetcode.com/problems/corporate-flight-bookings/solutions/1338804/java-2-approaches-brute-force-optimal-ap-e12t/
156+
// Optimized BRUTE FORCE
157+
public int[] corpFlightBookings_3_2(int[][] bookings, int n) {
56158

57-
// V1
159+
int[] res = new int[n];
160+
for (int[] curr : bookings) {
161+
int start = curr[0] - 1;
162+
int end = curr[1];
163+
int val = curr[2];
164+
res[start] += val;
165+
if (end < n) {
166+
res[end] -= val;
167+
}
168+
}
169+
for (int i = 1; i < n; i++) {
170+
res[i] += res[i - 1];
171+
}
172+
return res;
173+
}
58174

59-
// V2
60175
}

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev;
22

3+
import javax.print.DocFlavor;
34
import java.util.*;
45

56
public class workspace6 {
@@ -223,4 +224,100 @@ private boolean canBuildSqaure(int[] x1, int[] x2){
223224
return true;
224225
}
225226

227+
// LC 1109
228+
// https://leetcode.com/problems/corporate-flight-bookings/
229+
// 2.49 pm - 3.10 pm
230+
/**
231+
* 示例 1:
232+
*
233+
* 输入:bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
234+
* 输出:[10,55,45,25,25]
235+
* 解释:
236+
* 航班编号 1 2 3 4 5
237+
* 预订记录 1 : 10 10
238+
* 预订记录 2 : 20 20
239+
* 预订记录 3 : 25 25 25 25
240+
* 总座位数: 10 55 45 25 25 <-----------
241+
* 因此,answer = [10,55,45,25,25]
242+
*
243+
*
244+
* 示例 2:
245+
*
246+
* 输入:bookings = [[1,2,10],[2,2,15]], n = 2
247+
* 输出:[10,25]
248+
* 解释:
249+
* 航班编号 1 2
250+
* 预订记录 1 : 10 10
251+
* 预订记录 2 : 15
252+
* 总座位数: 10 25 <-----------
253+
* 因此,answer = [10,25]
254+
*
255+
*/
256+
/**
257+
* Idea : array op (presum ??)
258+
*
259+
* Exp 1: bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
260+
*
261+
* (idx start from 1)
262+
*
263+
*
264+
* * 输入:bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
265+
* * 输出:[10,55,45,25,25]
266+
* * 解释:
267+
* * 航班编号 1 2 3 4 5
268+
* * 预订记录 1 : 10 10
269+
* * 预订记录 2 : 20 20
270+
* * 预订记录 3 : 25 25 25 25
271+
* * 总座位数: 10 55 45 25 25 <-----------
272+
* * 因此,answer = [10,55,45,25,25]
273+
*
274+
*
275+
* -> arr1 = [10,10,0,0,0], presum1 = [10,20,20,20,20]
276+
* -> arr2 = [0,20,20,0,0], presum2 = [0,20,40,40,40]
277+
* -> arr3 = [0,25,25,25,25], presum = [0,25,50,75,100]
278+
* ...
279+
*
280+
*
281+
*/
282+
public int[] corpFlightBookings(int[][] bookings, int n) {
283+
int[] res = new int[n];
284+
List<Integer[]> preSumList = new ArrayList<>();
285+
for (int[] x : bookings){
286+
Integer[] preSumArr = createPreSumArr(x, n);
287+
System.out.println(">>> preSumArr = " + Arrays.asList(preSumArr));
288+
preSumList.add(createPreSumArr(x, n));
289+
}
290+
291+
// merge presum array to a single array
292+
List<Integer> mergedPreSum = new ArrayList<>();
293+
// TODO : optimize double loop ??
294+
for (Integer[] list : preSumList){{
295+
int cur = 0;
296+
for (int i = 0; i < list.length; i++){
297+
cur = mergedPreSum.get(i); // ???
298+
cur += list[i];
299+
// mergedPresum.add(cur);
300+
}
301+
mergedPreSum.add(cur);
302+
}}
303+
304+
// add result to res
305+
for (int j = 0; j < mergedPreSum.size(); j++){
306+
res[j] = mergedPreSum.get(j+1) - mergedPreSum.get(j);
307+
}
308+
return res;
309+
}
310+
311+
312+
private Integer[] createPreSumArr(int[] input, int n){
313+
int presum = 0;
314+
Integer[] res = new Integer[n+1];
315+
res[0] = 0;
316+
for (int i = 0; i < input.length; i++){
317+
presum += input[i];
318+
res[i+1] = presum;
319+
}
320+
return res;
321+
}
322+
226323
}

0 commit comments

Comments
 (0)