Skip to content

Commit 09724b8

Browse files
committed
update 731 java, cheatsheet
1 parent ea20b9a commit 09724b8

File tree

6 files changed

+362
-243
lines changed

6 files changed

+362
-243
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@
271271
723 | [Candy Crush](https://leetcode.com/problems/candy-crush/) | [Python](./leetcode_python/Array/candy-crush.py) | _O((R * C)^2)_ | _O(1)_ | Medium |`complex`| AGAIN (not start)
272272
724 | [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) | [Python](./leetcode_python/Array/find-pivot-index.py) | _O(n)_ | _O(1)_ | Easy || OK*
273273
729 | [My Calendar I](https://leetcode.com/problems/my-calendar-i/) | [Python](./leetcode_python/Array/my-calendar-i.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/MyCalendar1.java) | _O(nlogn)_ | _O(n)_ | Medium |good basic, brute force, binary search, google| AGAIN**** (1)
274-
731 | [My Calendar II](https://leetcode.com/problems/my-calendar-ii/) | [Python](./leetcode_python/Array/my-calendar-ii.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/MyCalendar2.java) | _O(n^2)_ | _O(n)_ | Medium |`trick`, scanning line, google,| AGAIN******** (3)
274+
731 | [My Calendar II](https://leetcode.com/problems/my-calendar-ii/) | [Python](./leetcode_python/Array/my-calendar-ii.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/MyCalendar2.java) | _O(n^2)_ | _O(n)_ | Medium |`trick`, good trick, scanning line, google,| AGAIN******** (4)
275275
747 | [Largest Number At Least Twice of Others](https://leetcode.com/problems/largest-number-at-least-twice-of-others/) | [Python](./leetcode_python/Array/largest-number-at-least-twice-of-others.py) | _O(n)_ | _O(1)_ | Easy |`good basic`, `data structure`| OK*
276276
755 | [Pour Water](https://leetcode.com/problems/pour-water/) | [Python](./leetcode_python/Array/pour-water.py) | _O(v * n)_ | _O(1)_ | Medium |`complex`| AGAIN (not start)
277277
766 | [Toeplitz Matrix](https://leetcode.com/problems/toeplitz-matrix/) | [Python](./leetcode_python/Array/toeplitz-matrix.py) | _O(m * n)_ | _O(1)_ | Easy |`basic`, `matrix`, `google`| AGAIN* (2)

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+
# 2025-01-05
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2025-01-04
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20250105:
12
20250104: 1031,369
23
20250101: 729,731
34
20241231: 769,817,855(again)

doc/cheatsheet/scanning_line.md

Lines changed: 81 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -198,79 +198,89 @@ class Solution:
198198

199199
```java
200200
// java
201-
// LC 731 My Calendar II
202-
// V1-1
203-
// https://leetcode.com/problems/my-calendar-ii/editorial/
204-
// IDEA: Line Sweep (Scanning line)
201+
// LC 731
202+
// V0
203+
// IDEA: SCANNING LINE + CUSTOM SORTING (fixed by gpt)
204+
class MyCalendarTwo {
205+
206+
// Attributes
205207
/**
206-
* IDEA:
207-
*
208-
*
209-
* 1) Class `MyCalendarTwo` will have two data members,
210-
* `maxOverlappedBooking` which is the maximum number of
211-
* concurrent bookings possible at a time,
212-
* and `bookingCount` which is a map from integer to integer
213-
* with the time point as the key and number of bookings as the value.
214-
*
215-
*
216-
* 2) Initialize `maxOverlappedBooking` as 2, as we need to check for triple booking.
208+
* NOTE !!!
217209
*
218-
* 3) Define the function book(start, end) as:
219-
*
220-
* - Increase the number of bookings for the time start and decrease
221-
* the number of bookings for end by 1 in the map bookingCount.
222-
*
223-
* - Iterate over each key-value pair in the map
224-
* in ascending order of keys to find the prefix sum.
225-
* Add the value in the map to the count overlappedBooking.
226-
*
227-
* - If overlappedBooking is more than two, it implies that this
228-
* is triple booking. Hence, we should return false.
229-
* Also, we need to revert the changes in the map as this booking shouldn't be added.
230-
*
231-
* - If we reach here, it implies no triple booking and hence returns true.
232210
*
211+
* statusList: {time, status, curBooked}
212+
* time: start time or end time
213+
* status: 1 for start, -1 for end
233214
*/
234-
class MyCalendarTwo_1_1 {
235-
236-
237-
238-
private TreeMap<Integer, Integer> bookingCount;
239-
private int maxOverlappedBooking;
240-
241-
public MyCalendarTwo_1_1() {
242-
bookingCount = new TreeMap<>();
243-
maxOverlappedBooking = 2;
244-
}
245-
246-
public boolean book(int start, int end) {
247-
// Increase the booking count at 'start' and decrease at 'end'.
248-
bookingCount.put(start, bookingCount.getOrDefault(start, 0) + 1);
249-
bookingCount.put(end, bookingCount.getOrDefault(end, 0) - 1);
250-
251-
int overlappedBooking = 0;
252-
253-
// Calculate the prefix sum of bookings.
254-
for (Map.Entry<Integer, Integer> entry : bookingCount.entrySet()) {
255-
overlappedBooking += entry.getValue();
256-
257-
// If the number of overlaps exceeds the allowed limit, rollback and
258-
// return false.
259-
if (overlappedBooking > maxOverlappedBooking) {
260-
// Rollback changes.
261-
bookingCount.put(start, bookingCount.get(start) - 1);
262-
bookingCount.put(end, bookingCount.get(end) + 1);
263-
264-
// Clean up if the count becomes zero to maintain the map clean.
265-
if (bookingCount.get(start) == 0) {
266-
bookingCount.remove(start);
267-
}
268-
269-
return false;
270-
}
271-
}
272-
273-
return true;
274-
}
275-
}
215+
List<Integer[]> statusList;
216+
217+
// Constructor
218+
public MyCalendarTwo() {
219+
this.statusList = new ArrayList<>();
220+
}
221+
222+
public boolean book(int startTime, int endTime) {
223+
224+
// Create intervals
225+
/**
226+
* NOTE !!!
227+
*
228+
* 1) we can init array at once as `new Inteter[] {a,b,c};
229+
* 2) we init curStart, curEnd array at first
230+
*/
231+
Integer[] curStart = new Integer[] { startTime, 1, 0 }; // {time, status, placeholder}
232+
Integer[] curEnd = new Integer[] { endTime, -1, 0 }; // {time, status, placeholder}
233+
234+
// Temporarily add them to the list for simulation
235+
/**
236+
* NOTE !!!
237+
*
238+
* -> we add curStart, curEnd to statusList directly
239+
* -> if new time is `over 2 times overlap`, we can REMOVE them
240+
* from statusList and return `false` in this method,
241+
* and we can keep this method running and validate the
242+
* other input (startTime, endTime)
243+
*/
244+
statusList.add(curStart);
245+
statusList.add(curEnd);
246+
247+
// Sort by time, then by status (start before end)
248+
/**
249+
* NOTE !!!
250+
*
251+
* custom sorting
252+
*
253+
* -> so, sort time first,
254+
* if time are equal, then sort on `status` (0 or 1),
255+
* `open` goes first, `close` goes next
256+
*/
257+
statusList.sort((x, y) -> {
258+
if (!x[0].equals(y[0])) {
259+
return x[0] - y[0];
260+
}
261+
return x[1] - y[1]; // start (+1) comes before end (-1)
262+
});
263+
264+
// Scanning line to check overlaps
265+
int curBooked = 0;
266+
for (Integer[] interval : statusList) {
267+
curBooked += interval[1];
268+
if (curBooked >= 3) {
269+
// Remove the temporary intervals
270+
/**
271+
* NOTE !!!
272+
*
273+
* if overlap > 2, we just remove the new added times,
274+
* and return false as method response
275+
*/
276+
statusList.remove(curStart);
277+
statusList.remove(curEnd);
278+
return false; // Booking not allowed
279+
}
280+
}
281+
282+
// Booking is valid, keep the intervals
283+
return true;
284+
}
285+
}
276286
```

leetcode_java/src/main/java/LeetCodeJava/Array/MyCalendar2.java

Lines changed: 83 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -64,81 +64,89 @@ public class MyCalendar2 {
6464
*/
6565

6666
// V0
67-
// TODO: fix below
68-
// class MyCalendarTwo {
69-
//
70-
// List<List<Integer>> booked;
71-
// Map<List<Integer>, Integer> overlapCnt;
72-
//
73-
// public MyCalendarTwo() {
74-
// this.booked = new ArrayList<>();
75-
// this.overlapCnt = new HashMap<>();
76-
// }
77-
//
78-
// public boolean book(int startTime, int endTime) {
79-
//
80-
// List<Integer> tmp = new ArrayList<>();
81-
// tmp.add(startTime);
82-
// tmp.add(endTime);
83-
//
84-
// // case 1) booked is empty
85-
// if(this.booked.isEmpty()){
86-
// this.booked.add(tmp);
87-
// return true;
88-
// }
89-
//
90-
// boolean lessEqualsThreeOverlap = false;
91-
//
92-
//
93-
// for(List<Integer> x: this.booked){
94-
// /**
95-
// * |----|
96-
// * |------| (old)
97-
// *
98-
// * or
99-
// *
100-
// * |-----|
101-
// * |----| (old)
102-
// *
103-
// * or
104-
// *
105-
// * |---|
106-
// * |----------| (old)
107-
// *
108-
// *
109-
// */
110-
// int existingStart = x.get(0);
111-
// int existingEnd = x.get(1);
112-
//
113-
// if (startTime < existingEnd && existingStart < endTime) {
114-
// // case 2) has overlap, but `overlap count` <= 3
115-
// List<Integer> tmpExisting = new ArrayList<>();
116-
// tmpExisting.add(existingStart);
117-
// tmpExisting.add(existingEnd);
118-
// if(this.overlapCnt.get(tmpExisting) <= 3){
119-
// // update existing start, end
120-
// existingStart = Math.min(existingStart, startTime);
121-
// existingEnd = Math.max(existingEnd, endTime);
122-
// List<Integer> tmp2 = new ArrayList<>();
123-
// tmp2.add(existingStart);
124-
// tmp2.add(existingEnd);
125-
// this.overlapCnt.put(tmp2, this.overlapCnt.get(tmpExisting)+1); // update overlap cnt
126-
// this.overlapCnt.remove(tmpExisting);
127-
// return true;
128-
// }else{
129-
// // case 3) has overlap, and `overlap count` > 3
130-
// return false;
131-
// }
132-
// }
133-
//
134-
// }
135-
//
136-
// // case 4) no overlap
137-
// this.booked.add(tmp);
138-
// this.overlapCnt.put(tmp, 1); // update overlap cnt
139-
// return true;
140-
// }
141-
// }
67+
// IDEA: SCANNING LINE + CUSTOM SORTING (fixed by gpt)
68+
class MyCalendarTwo {
69+
70+
// Attributes
71+
/**
72+
* NOTE !!!
73+
*
74+
*
75+
* statusList: {time, status, curBooked}
76+
* time: start time or end time
77+
* status: 1 for start, -1 for end
78+
*/
79+
List<Integer[]> statusList;
80+
81+
// Constructor
82+
public MyCalendarTwo() {
83+
this.statusList = new ArrayList<>();
84+
}
85+
86+
public boolean book(int startTime, int endTime) {
87+
88+
// Create intervals
89+
/**
90+
* NOTE !!!
91+
*
92+
* 1) we can init array at once as `new Inteter[] {a,b,c};
93+
* 2) we init curStart, curEnd array at first
94+
*/
95+
Integer[] curStart = new Integer[] { startTime, 1, 0 }; // {time, status, placeholder}
96+
Integer[] curEnd = new Integer[] { endTime, -1, 0 }; // {time, status, placeholder}
97+
98+
// Temporarily add them to the list for simulation
99+
/**
100+
* NOTE !!!
101+
*
102+
* -> we add curStart, curEnd to statusList directly
103+
* -> if new time is `over 2 times overlap`, we can REMOVE them
104+
* from statusList and return `false` in this method,
105+
* and we can keep this method running and validate the
106+
* other input (startTime, endTime)
107+
*/
108+
statusList.add(curStart);
109+
statusList.add(curEnd);
110+
111+
// Sort by time, then by status (start before end)
112+
/**
113+
* NOTE !!!
114+
*
115+
* custom sorting
116+
*
117+
* -> so, sort time first,
118+
* if time are equal, then sort on `status` (0 or 1),
119+
* `open` goes first, `close` goes next
120+
*/
121+
statusList.sort((x, y) -> {
122+
if (!x[0].equals(y[0])) {
123+
return x[0] - y[0];
124+
}
125+
return x[1] - y[1]; // start (+1) comes before end (-1)
126+
});
127+
128+
// Scanning line to check overlaps
129+
int curBooked = 0;
130+
for (Integer[] interval : statusList) {
131+
curBooked += interval[1];
132+
if (curBooked >= 3) {
133+
// Remove the temporary intervals
134+
/**
135+
* NOTE !!!
136+
*
137+
* if overlap > 2, we just remove the new added times,
138+
* and return false as method response
139+
*/
140+
statusList.remove(curStart);
141+
statusList.remove(curEnd);
142+
return false; // Booking not allowed
143+
}
144+
}
145+
146+
// Booking is valid, keep the intervals
147+
return true;
148+
}
149+
}
142150

143151
// V1-1
144152
// https://leetcode.com/problems/my-calendar-ii/editorial/

0 commit comments

Comments
 (0)