Skip to content

Commit 6897c23

Browse files
committed
update 523 java, progress, add 1109 java
1 parent 8b0f6d6 commit 6897c23

File tree

5 files changed

+206
-14
lines changed

5 files changed

+206
-14
lines changed

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2024-12-15
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-12-08
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
20241215: 1109
12
20241214: 560,523
23
20241208: 304,853,325
3-
20241202: 370(todo),1109(todo)
4+
20241202: 370(todo)
45
20241130: 34,767
56
20241126: 722,380
67
20241125: 33,81
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package LeetCodeJava.Array;
2+
3+
// https://leetcode.com/problems/corporate-flight-bookings/description/
4+
/**
5+
* 1109. Corporate Flight Bookings
6+
* Solved
7+
* Medium
8+
* Topics
9+
* Companies
10+
* There are n flights that are labeled from 1 to n.
11+
*
12+
* 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.
13+
*
14+
* Return an array answer of length n, where answer[i] is the total number of seats reserved for flight i.
15+
*
16+
*
17+
*
18+
* Example 1:
19+
*
20+
* Input: bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
21+
* Output: [10,55,45,25,25]
22+
* Explanation:
23+
* Flight labels: 1 2 3 4 5
24+
* Booking 1 reserved: 10 10
25+
* Booking 2 reserved: 20 20
26+
* Booking 3 reserved: 25 25 25 25
27+
* Total seats: 10 55 45 25 25
28+
* Hence, answer = [10,55,45,25,25]
29+
* Example 2:
30+
*
31+
* Input: bookings = [[1,2,10],[2,2,15]], n = 2
32+
* Output: [10,25]
33+
* Explanation:
34+
* Flight labels: 1 2
35+
* Booking 1 reserved: 10 10
36+
* Booking 2 reserved: 15
37+
* Total seats: 10 25
38+
* Hence, answer = [10,25]
39+
*
40+
*
41+
*
42+
* Constraints:
43+
*
44+
* 1 <= n <= 2 * 104
45+
* 1 <= bookings.length <= 2 * 104
46+
* bookings[i].length == 3
47+
* 1 <= firsti <= lasti <= n
48+
* 1 <= seatsi <= 104
49+
*/
50+
public class CorporateFlightBookings {
51+
52+
// V0
53+
// public int[] corpFlightBookings(int[][] bookings, int n) {
54+
//
55+
// }
56+
57+
// V1
58+
59+
// V2
60+
}

leetcode_java/src/main/java/LeetCodeJava/HashTable/ContinuousSubarraySum.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,72 @@ public boolean checkSubarraySum(int[] nums, int k) {
9393
return false;
9494
}
9595

96+
// V0-1
97+
public boolean checkSubarraySum_0_1(int[] nums, int k) {
98+
99+
if (nums.length < 2){
100+
return false;
101+
}
102+
103+
// map : {preSum: idx}
104+
Map<Integer, Integer> map = new HashMap<>();
105+
106+
// init the value with init status
107+
map.put(0,-1);
108+
int presum = 0;
109+
110+
for (int i = 0; i < nums.length; i++){
111+
int cur = nums[i];
112+
/**
113+
*
114+
* NOTE !!!!!
115+
*
116+
*
117+
* sum(i,j) = presum(j+1) - presum(i)
118+
*
119+
*
120+
* remainder : presum = (presum + cur) % k
121+
*
122+
* so if map has "key" with remainder value (AKA (presum + cur) % k)
123+
* -> we found a sub-array sum that is multiple of k
124+
*
125+
*
126+
* e.g. if remainder = (presum + cur) % k
127+
* -> we can find a subarray with k multiple
128+
*
129+
* -> idea 1) (presum + cur) % k - (presum) = 0, so it's k multiple
130+
*
131+
*
132+
*/
133+
presum = (presum + cur) % k;
134+
// NOTE !!! below
135+
if (map.containsKey(presum)){
136+
if (i - map.get(presum) > 1){ // check is sub array length >= 2
137+
return true;
138+
}
139+
}
140+
map.putIfAbsent(presum, i);
141+
142+
143+
// BELOW is wrong !!!
144+
// map.putIfAbsent(presum, i);
145+
// /**
146+
// * sum(i,j) = presum(j+1) - presum(i)
147+
// *
148+
// * -> presum(j+1) - presum(i) = k
149+
// * -> so, presum(i) = presum(j+1) - k
150+
// *
151+
// * similar idea for "presum mod"
152+
// */
153+
// if(map.containsKey(presum - k) && map.get(presum - k) - i >= 2){
154+
// return true;
155+
// }
156+
}
157+
158+
return false;
159+
}
160+
161+
96162
// V1
97163
// IDEA : HASHMAP
98164
// https://leetcode.com/problems/continuous-subarray-sum/editorial/

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4417,38 +4417,93 @@ public int maxSubArrayLen(int[] nums, int k) {
44174417
* the sum of the elements of the subarray is a multiple of k.
44184418
*
44194419
*/
4420+
// 8,30 am - 8.40 am
44204421
public boolean checkSubarraySum(int[] nums, int k) {
4422+
44214423
if (nums.length < 2){
44224424
return false;
44234425
}
4424-
// hash map
4425-
// record presum and idx
4426-
// map(presum: idx)
4426+
4427+
// map : {preSum: idx}
44274428
Map<Integer, Integer> map = new HashMap<>();
4429+
4430+
// init the value with init status
4431+
map.put(0,-1);
44284432
int presum = 0;
4433+
44294434
for (int i = 0; i < nums.length; i++){
44304435
int cur = nums[i];
4431-
// if there is any element equals 0, return true directly
4432-
// if (cur == 0){
4433-
// return true;
4434-
// }
4435-
presum += cur;
4436-
map.putIfAbsent(presum, i);
44374436
/**
4437+
*
44384438
* sum(i,j) = presum(j+1) - presum(i)
44394439
*
4440-
* k = presum(j+1) - presum(i)
4441-
* -> presum(i) = presum(j+1) - k
4440+
*
4441+
* remainder : presum = (presum + cur) % k
4442+
*
4443+
* so if map has "key" with remainder value (AKA (presum + cur) % k)
4444+
* -> we found a sub-array sum that is multiple of k
4445+
*
44424446
*/
4443-
if (map.containsKey(presum - k) || presum % k == 0){
4444-
return true;
4447+
presum = (presum + cur) % k; // TODO : double check
4448+
if (map.containsKey(presum)){
4449+
if (i - map.get(presum) > 1){ // check is sub array length >= 2
4450+
return true;
4451+
}
44454452
}
4453+
map.putIfAbsent(presum, i);
4454+
44464455

4456+
// BELOW is wrong !!!
4457+
// map.putIfAbsent(presum, i);
4458+
// /**
4459+
// * sum(i,j) = presum(j+1) - presum(i)
4460+
// *
4461+
// * -> presum(j+1) - presum(i) = k
4462+
// * -> so, presum(i) = presum(j+1) - k
4463+
// *
4464+
// * similar idea for "presum mod"
4465+
// */
4466+
// if(map.containsKey(presum - k) && map.get(presum - k) - i >= 2){
4467+
// return true;
4468+
// }
44474469
}
44484470

44494471
return false;
44504472
}
44514473

4474+
4475+
// public boolean checkSubarraySum(int[] nums, int k) {
4476+
// if (nums.length < 2){
4477+
// return false;
4478+
// }
4479+
// // hash map
4480+
// // record presum and idx
4481+
// // map(presum: idx)
4482+
// Map<Integer, Integer> map = new HashMap<>();
4483+
// int presum = 0;
4484+
// for (int i = 0; i < nums.length; i++){
4485+
// int cur = nums[i];
4486+
// // if there is any element equals 0, return true directly
4487+
//// if (cur == 0){
4488+
//// return true;
4489+
//// }
4490+
// presum += cur;
4491+
// map.putIfAbsent(presum, i);
4492+
// /**
4493+
// * sum(i,j) = presum(j+1) - presum(i)
4494+
// *
4495+
// * k = presum(j+1) - presum(i)
4496+
// * -> presum(i) = presum(j+1) - k
4497+
// */
4498+
// if (map.containsKey(presum - k) || presum % k == 0){
4499+
// return true;
4500+
// }
4501+
//
4502+
// }
4503+
//
4504+
// return false;
4505+
// }
4506+
44524507
// LC 560
44534508
// https://leetcode.com/problems/subarray-sum-equals-k/
44544509
// 11.18 AM - 11.40 AM
@@ -4489,6 +4544,13 @@ public int subarraySum(int[] nums, int k) {
44894544
return cnt;
44904545
}
44914546

4547+
// LC 1109
4548+
// https://leetcode.com/problems/corporate-flight-bookings/
4549+
public int[] corpFlightBookings(int[][] bookings, int n) {
4550+
4551+
return null;
4552+
}
4553+
44924554

44934555

44944556
}

0 commit comments

Comments
 (0)