diff --git a/data/progress.md b/data/progress.md index f04577b7..a2dfcc0a 100644 --- a/data/progress.md +++ b/data/progress.md @@ -1,5 +1,8 @@ # Progress +# 2024-12-15 +- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf + # 2024-12-08 - https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf diff --git a/data/progress.txt b/data/progress.txt index 824857b2..ecbb07da 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,6 +1,7 @@ +20241215: 1109 20241214: 560,523 20241208: 304,853,325 -20241202: 370(todo),1109(todo) +20241202: 370(todo) 20241130: 34,767 20241126: 722,380 20241125: 33,81 diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/CorporateFlightBookings.java b/leetcode_java/src/main/java/LeetCodeJava/Array/CorporateFlightBookings.java new file mode 100644 index 00000000..95622891 --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/Array/CorporateFlightBookings.java @@ -0,0 +1,60 @@ +package LeetCodeJava.Array; + +// https://leetcode.com/problems/corporate-flight-bookings/description/ +/** + * 1109. Corporate Flight Bookings + * Solved + * Medium + * Topics + * Companies + * There are n flights that are labeled from 1 to n. + * + * You are given an array of flight bookings bookings, where bookings[i] = [firsti, lasti, seatsi] represents a booking for flights firsti through lasti (inclusive) with seatsi seats reserved for each flight in the range. + * + * Return an array answer of length n, where answer[i] is the total number of seats reserved for flight i. + * + * + * + * Example 1: + * + * Input: bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5 + * Output: [10,55,45,25,25] + * Explanation: + * Flight labels: 1 2 3 4 5 + * Booking 1 reserved: 10 10 + * Booking 2 reserved: 20 20 + * Booking 3 reserved: 25 25 25 25 + * Total seats: 10 55 45 25 25 + * Hence, answer = [10,55,45,25,25] + * Example 2: + * + * Input: bookings = [[1,2,10],[2,2,15]], n = 2 + * Output: [10,25] + * Explanation: + * Flight labels: 1 2 + * Booking 1 reserved: 10 10 + * Booking 2 reserved: 15 + * Total seats: 10 25 + * Hence, answer = [10,25] + * + * + * + * Constraints: + * + * 1 <= n <= 2 * 104 + * 1 <= bookings.length <= 2 * 104 + * bookings[i].length == 3 + * 1 <= firsti <= lasti <= n + * 1 <= seatsi <= 104 + */ +public class CorporateFlightBookings { + + // V0 +// public int[] corpFlightBookings(int[][] bookings, int n) { +// +// } + + // V1 + + // V2 +} diff --git a/leetcode_java/src/main/java/LeetCodeJava/HashTable/ContinuousSubarraySum.java b/leetcode_java/src/main/java/LeetCodeJava/HashTable/ContinuousSubarraySum.java index 4dbb0383..7867f54f 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/HashTable/ContinuousSubarraySum.java +++ b/leetcode_java/src/main/java/LeetCodeJava/HashTable/ContinuousSubarraySum.java @@ -93,6 +93,72 @@ public boolean checkSubarraySum(int[] nums, int k) { return false; } + // V0-1 + public boolean checkSubarraySum_0_1(int[] nums, int k) { + + if (nums.length < 2){ + return false; + } + + // map : {preSum: idx} + Map map = new HashMap<>(); + + // init the value with init status + map.put(0,-1); + int presum = 0; + + for (int i = 0; i < nums.length; i++){ + int cur = nums[i]; + /** + * + * NOTE !!!!! + * + * + * sum(i,j) = presum(j+1) - presum(i) + * + * + * remainder : presum = (presum + cur) % k + * + * so if map has "key" with remainder value (AKA (presum + cur) % k) + * -> we found a sub-array sum that is multiple of k + * + * + * e.g. if remainder = (presum + cur) % k + * -> we can find a subarray with k multiple + * + * -> idea 1) (presum + cur) % k - (presum) = 0, so it's k multiple + * + * + */ + presum = (presum + cur) % k; + // NOTE !!! below + if (map.containsKey(presum)){ + if (i - map.get(presum) > 1){ // check is sub array length >= 2 + return true; + } + } + map.putIfAbsent(presum, i); + + + // BELOW is wrong !!! +// map.putIfAbsent(presum, i); +// /** +// * sum(i,j) = presum(j+1) - presum(i) +// * +// * -> presum(j+1) - presum(i) = k +// * -> so, presum(i) = presum(j+1) - k +// * +// * similar idea for "presum mod" +// */ +// if(map.containsKey(presum - k) && map.get(presum - k) - i >= 2){ +// return true; +// } + } + + return false; + } + + // V1 // IDEA : HASHMAP // https://leetcode.com/problems/continuous-subarray-sum/editorial/ diff --git a/leetcode_java/src/main/java/dev/workspace5.java b/leetcode_java/src/main/java/dev/workspace5.java index 8924ae7b..8b338c0e 100644 --- a/leetcode_java/src/main/java/dev/workspace5.java +++ b/leetcode_java/src/main/java/dev/workspace5.java @@ -4417,38 +4417,93 @@ public int maxSubArrayLen(int[] nums, int k) { * the sum of the elements of the subarray is a multiple of k. * */ + // 8,30 am - 8.40 am public boolean checkSubarraySum(int[] nums, int k) { + if (nums.length < 2){ return false; } - // hash map - // record presum and idx - // map(presum: idx) + + // map : {preSum: idx} Map map = new HashMap<>(); + + // init the value with init status + map.put(0,-1); int presum = 0; + for (int i = 0; i < nums.length; i++){ int cur = nums[i]; - // if there is any element equals 0, return true directly -// if (cur == 0){ -// return true; -// } - presum += cur; - map.putIfAbsent(presum, i); /** + * * sum(i,j) = presum(j+1) - presum(i) * - * k = presum(j+1) - presum(i) - * -> presum(i) = presum(j+1) - k + * + * remainder : presum = (presum + cur) % k + * + * so if map has "key" with remainder value (AKA (presum + cur) % k) + * -> we found a sub-array sum that is multiple of k + * */ - if (map.containsKey(presum - k) || presum % k == 0){ - return true; + presum = (presum + cur) % k; // TODO : double check + if (map.containsKey(presum)){ + if (i - map.get(presum) > 1){ // check is sub array length >= 2 + return true; + } } + map.putIfAbsent(presum, i); + + // BELOW is wrong !!! +// map.putIfAbsent(presum, i); +// /** +// * sum(i,j) = presum(j+1) - presum(i) +// * +// * -> presum(j+1) - presum(i) = k +// * -> so, presum(i) = presum(j+1) - k +// * +// * similar idea for "presum mod" +// */ +// if(map.containsKey(presum - k) && map.get(presum - k) - i >= 2){ +// return true; +// } } return false; } + +// public boolean checkSubarraySum(int[] nums, int k) { +// if (nums.length < 2){ +// return false; +// } +// // hash map +// // record presum and idx +// // map(presum: idx) +// Map map = new HashMap<>(); +// int presum = 0; +// for (int i = 0; i < nums.length; i++){ +// int cur = nums[i]; +// // if there is any element equals 0, return true directly +//// if (cur == 0){ +//// return true; +//// } +// presum += cur; +// map.putIfAbsent(presum, i); +// /** +// * sum(i,j) = presum(j+1) - presum(i) +// * +// * k = presum(j+1) - presum(i) +// * -> presum(i) = presum(j+1) - k +// */ +// if (map.containsKey(presum - k) || presum % k == 0){ +// return true; +// } +// +// } +// +// return false; +// } + // LC 560 // https://leetcode.com/problems/subarray-sum-equals-k/ // 11.18 AM - 11.40 AM @@ -4489,6 +4544,13 @@ public int subarraySum(int[] nums, int k) { return cnt; } + // LC 1109 + // https://leetcode.com/problems/corporate-flight-bookings/ + public int[] corpFlightBookings(int[][] bookings, int n) { + + return null; + } + }