Skip to content

Commit

Permalink
update 523 java, progress, add 1109 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 15, 2024
1 parent 8b0f6d6 commit 6897c23
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 14 deletions.
3 changes: 3 additions & 0 deletions data/progress.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
3 changes: 2 additions & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, Integer> 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/
Expand Down
88 changes: 75 additions & 13 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, Integer> 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<Integer, Integer> 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
Expand Down Expand Up @@ -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;
}



}
Expand Down

0 comments on commit 6897c23

Please sign in to comment.