Skip to content

Commit

Permalink
add 1109 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 20, 2024
1 parent 85def7f commit b9799db
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@
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)
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)
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)
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)
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)
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)
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)
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)
Expand Down
3 changes: 1 addition & 2 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
20241220: 815,871,593
20241215: 1109
20241220: 815,871,593,1109
20241214: 560,523
20241208: 304,853,325
20241202: 370(todo)
Expand Down
30 changes: 14 additions & 16 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
2025-02-13 -> ['815,871,593']
2025-02-08 -> ['1109']
2025-02-13 -> ['815,871,593,1109']
2025-02-07 -> ['560,523']
2025-02-01 -> ['304,853,325']
2025-01-26 -> ['370(todo)']
2025-01-24 -> ['34,767']
2025-01-23 -> ['815,871,593']
2025-01-23 -> ['815,871,593,1109']
2025-01-20 -> ['722,380']
2025-01-19 -> ['33,81']
2025-01-18 -> ['1109']
2025-01-17 -> ['560,523', '253']
2025-01-16 -> ['776,31']
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2025-01-11 -> ['304,853,325', '394']
2025-01-10 -> ['815,871,593', '833,950']
2025-01-05 -> ['1109', '370(todo)']
2025-01-10 -> ['815,871,593,1109', '833,950']
2025-01-05 -> ['370(todo)']
2025-01-04 -> ['560,523', '53,210,207']
2025-01-03 -> ['34,767', '444']
2025-01-02 -> ['815,871,593', '1188,130,855(again)']
2025-01-02 -> ['815,871,593,1109', '1188,130,855(again)']
2024-12-30 -> ['722,380']
2024-12-29 -> ['304,853,325', '33,81']
2024-12-28 -> ['815,871,593', '1109', '900']
2024-12-28 -> ['815,871,593,1109', '900']
2024-12-27 -> ['560,523', '253', '26,27', '802,1197,26']
2024-12-26 -> ['776,31']
2024-12-25 -> ['815,871,593', '004(todo),34(todo),162(todo),275(todo)']
2024-12-25 -> ['815,871,593,1109', '004(todo),34(todo),162(todo),275(todo)']
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-23 -> ['815,871,593', '1109', '370(todo)']
2024-12-22 -> ['815,871,593', '560,523']
2024-12-21 -> ['815,871,593', '304,853,325', '34,767', '394', '855,846']
2024-12-20 -> ['1109', '833,950', '932']
2024-12-23 -> ['815,871,593,1109', '370(todo)']
2024-12-22 -> ['815,871,593,1109', '560,523']
2024-12-21 -> ['815,871,593,1109', '304,853,325', '34,767', '394', '855,846']
2024-12-20 -> ['833,950', '932']
2024-12-19 -> ['560,523']
2024-12-18 -> ['1109', '951,792']
2024-12-17 -> ['1109', '560,523', '722,380']
2024-12-16 -> ['1109', '560,523', '304,853,325', '33,81']
2024-12-18 -> ['951,792']
2024-12-17 -> ['560,523', '722,380']
2024-12-16 -> ['560,523', '304,853,325', '33,81']
2024-12-15 -> ['560,523', '370(todo)']
2024-12-14 -> ['253', '53,210,207', '163,1048']
2024-12-13 -> ['304,853,325', '34,767', '776,31', '444', '298,729']
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package LeetCodeJava.Array;

// https://leetcode.com/problems/corporate-flight-bookings/description/
// https://leetcode.cn/problems/corporate-flight-bookings/description/

import java.util.Arrays;

/**
* 1109. Corporate Flight Bookings
* Solved
Expand Down Expand Up @@ -46,15 +50,126 @@
* bookings[i].length == 3
* 1 <= firsti <= lasti <= n
* 1 <= seatsi <= 104
*
*
* Example :
*
*
* 示例 1:
*
* 输入:bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
* 输出:[10,55,45,25,25]
* 解释:
* 航班编号 1 2 3 4 5
* 预订记录 1 : 10 10
* 预订记录 2 : 20 20
* 预订记录 3 : 25 25 25 25
* 总座位数: 10 55 45 25 25
* 因此,answer = [10,55,45,25,25]
* 示例 2:
*
* 输入:bookings = [[1,2,10],[2,2,15]], n = 2
* 输出:[10,25]
* 解释:
* 航班编号 1 2
* 预订记录 1 : 10 10
* 预订记录 2 : 15
* 总座位数: 10 25
* 因此,answer = [10,25]
*
*/
public class CorporateFlightBookings {

// V0
// public int[] corpFlightBookings(int[][] bookings, int n) {
//
// }
// V0
// TODO : implement
// public int[] corpFlightBookings(int[][] bookings, int n) {
//
// }

// V1
// IDEA : difference ARRAY (gpt)
public int[] corpFlightBookings_1(int[][] bookings, int n) {
int[] res = new int[n + 1]; // Use n+1 to handle range updates easily with a difference array.

// Create a difference array
for (int[] booking : bookings) {
int start = booking[0]; // Starting index (1-based)
int end = booking[1]; // Ending index (1-based)
int seats = booking[2]; // Seats to add

res[start] += seats; // Add seats at the start
if (end + 1 <= n) { // Subtract seats after the end
res[end + 1] -= seats;
}
}

// Compute the prefix sum to get the final result
for (int i = 1; i <= n; i++) {
res[i] += res[i - 1];
}

// Exclude the extra index used for the difference array
return Arrays.copyOfRange(res, 1, n + 1);
}

// V2
// https://leetcode.com/problems/corporate-flight-bookings/submissions/1483568206/
public int[] corpFlightBookings_2(int[][] bookings, int n) {
int[] ans = new int[n];
for (int[] booking : bookings) {
int i = booking[0] - 1;
int j = booking[1];
int seats = booking[2];
ans[i] += seats;
if (j != n)
ans[j] -= seats;
}

int count = 0;
for (int i = 0; i < ans.length; i++) {
ans[i] += count;
count = ans[i];
}
return ans;
}

// V3-1
// https://leetcode.com/problems/corporate-flight-bookings/solutions/1338804/java-2-approaches-brute-force-optimal-ap-e12t/
// IDEA : BRUTE FORCE
public int[] corpFlightBookings_3_1(int[][] bookings, int n) {

int[] res = new int[n];
for (int[] curr : bookings) {
int start = curr[0] - 1;
int end = curr[1];
int val = curr[2];

for (int i = start; i < end; i++) {
res[i] += val;
}
}
return res;
}

// V3-2
// https://leetcode.com/problems/corporate-flight-bookings/solutions/1338804/java-2-approaches-brute-force-optimal-ap-e12t/
// Optimized BRUTE FORCE
public int[] corpFlightBookings_3_2(int[][] bookings, int n) {

// V1
int[] res = new int[n];
for (int[] curr : bookings) {
int start = curr[0] - 1;
int end = curr[1];
int val = curr[2];
res[start] += val;
if (end < n) {
res[end] -= val;
}
}
for (int i = 1; i < n; i++) {
res[i] += res[i - 1];
}
return res;
}

// V2
}
97 changes: 97 additions & 0 deletions leetcode_java/src/main/java/dev/workspace6.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev;

import javax.print.DocFlavor;
import java.util.*;

public class workspace6 {
Expand Down Expand Up @@ -223,4 +224,100 @@ private boolean canBuildSqaure(int[] x1, int[] x2){
return true;
}

// LC 1109
// https://leetcode.com/problems/corporate-flight-bookings/
// 2.49 pm - 3.10 pm
/**
* 示例 1:
*
* 输入:bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
* 输出:[10,55,45,25,25]
* 解释:
* 航班编号 1 2 3 4 5
* 预订记录 1 : 10 10
* 预订记录 2 : 20 20
* 预订记录 3 : 25 25 25 25
* 总座位数: 10 55 45 25 25 <-----------
* 因此,answer = [10,55,45,25,25]
*
*
* 示例 2:
*
* 输入:bookings = [[1,2,10],[2,2,15]], n = 2
* 输出:[10,25]
* 解释:
* 航班编号 1 2
* 预订记录 1 : 10 10
* 预订记录 2 : 15
* 总座位数: 10 25 <-----------
* 因此,answer = [10,25]
*
*/
/**
* Idea : array op (presum ??)
*
* Exp 1: bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
*
* (idx start from 1)
*
*
* * 输入:bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
* * 输出:[10,55,45,25,25]
* * 解释:
* * 航班编号 1 2 3 4 5
* * 预订记录 1 : 10 10
* * 预订记录 2 : 20 20
* * 预订记录 3 : 25 25 25 25
* * 总座位数: 10 55 45 25 25 <-----------
* * 因此,answer = [10,55,45,25,25]
*
*
* -> arr1 = [10,10,0,0,0], presum1 = [10,20,20,20,20]
* -> arr2 = [0,20,20,0,0], presum2 = [0,20,40,40,40]
* -> arr3 = [0,25,25,25,25], presum = [0,25,50,75,100]
* ...
*
*
*/
public int[] corpFlightBookings(int[][] bookings, int n) {
int[] res = new int[n];
List<Integer[]> preSumList = new ArrayList<>();
for (int[] x : bookings){
Integer[] preSumArr = createPreSumArr(x, n);
System.out.println(">>> preSumArr = " + Arrays.asList(preSumArr));
preSumList.add(createPreSumArr(x, n));
}

// merge presum array to a single array
List<Integer> mergedPreSum = new ArrayList<>();
// TODO : optimize double loop ??
for (Integer[] list : preSumList){{
int cur = 0;
for (int i = 0; i < list.length; i++){
cur = mergedPreSum.get(i); // ???
cur += list[i];
// mergedPresum.add(cur);
}
mergedPreSum.add(cur);
}}

// add result to res
for (int j = 0; j < mergedPreSum.size(); j++){
res[j] = mergedPreSum.get(j+1) - mergedPreSum.get(j);
}
return res;
}


private Integer[] createPreSumArr(int[] input, int n){
int presum = 0;
Integer[] res = new Integer[n+1];
res[0] = 0;
for (int i = 0; i < input.length; i++){
presum += input[i];
res[i+1] = presum;
}
return res;
}

}

0 comments on commit b9799db

Please sign in to comment.