diff --git a/README.md b/README.md index 00c90c4b..aa02c347 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/data/progress.txt b/data/progress.txt index 497711cd..266a4bf3 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -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) diff --git a/data/to_review.txt b/data/to_review.txt index c394a913..b4871aa5 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -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'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/CorporateFlightBookings.java b/leetcode_java/src/main/java/LeetCodeJava/Array/CorporateFlightBookings.java index 95622891..ec4f51b0 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/Array/CorporateFlightBookings.java +++ b/leetcode_java/src/main/java/LeetCodeJava/Array/CorporateFlightBookings.java @@ -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 @@ -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 } diff --git a/leetcode_java/src/main/java/dev/workspace6.java b/leetcode_java/src/main/java/dev/workspace6.java index ea13c635..34a1d95c 100644 --- a/leetcode_java/src/main/java/dev/workspace6.java +++ b/leetcode_java/src/main/java/dev/workspace6.java @@ -1,5 +1,6 @@ package dev; +import javax.print.DocFlavor; import java.util.*; public class workspace6 { @@ -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 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 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; + } + }