Skip to content

Commit e20dd82

Browse files
committed
add 731 java
1 parent a29d1b4 commit e20dd82

File tree

4 files changed

+262
-8
lines changed

4 files changed

+262
-8
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) | _O(n^2)_ | _O(n)_ | Medium |`trick`, `basic`| AGAIN** (not start)
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`, `basic`, google,| AGAIN** (2)
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/to_review.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2025-02-25 -> ['729,731']
12
2025-02-24 -> ['769,817,855(again)']
23
2025-02-22 -> ['498(again),934']
34
2025-02-21 -> ['379,173']
@@ -6,6 +7,7 @@
67
2025-02-14 -> ['370']
78
2025-02-13 -> ['815,871,593,1109']
89
2025-02-07 -> ['560,523']
10+
2025-02-04 -> ['729,731']
911
2025-02-03 -> ['769,817,855(again)']
1012
2025-02-01 -> ['498(again),934', '304,853,325']
1113
2025-01-31 -> ['379,173']
@@ -14,25 +16,26 @@
1416
2025-01-25 -> ['369,311']
1517
2025-01-24 -> ['370', '34,767']
1618
2025-01-23 -> ['815,871,593,1109']
19+
2025-01-22 -> ['729,731']
1720
2025-01-21 -> ['769,817,855(again)']
1821
2025-01-20 -> ['722,380']
1922
2025-01-19 -> ['498(again),934', '33,81']
2023
2025-01-18 -> ['379,173']
2124
2025-01-17 -> ['079,212(todo),362,849', '560,523', '253']
2225
2025-01-16 -> ['776,31']
2326
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
24-
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
27+
2025-01-14 -> ['729,731', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2528
2025-01-13 -> ['769,817,855(again)']
2629
2025-01-12 -> ['369,311']
2730
2025-01-11 -> ['498(again),934', '370', '304,853,325', '394']
2831
2025-01-10 -> ['379,173', '815,871,593,1109', '833,950']
29-
2025-01-09 -> ['079,212(todo),362,849']
32+
2025-01-09 -> ['729,731', '079,212(todo),362,849']
3033
2025-01-08 -> ['769,817,855(again)']
31-
2025-01-06 -> ['498(again),934']
34+
2025-01-06 -> ['729,731', '498(again),934']
3235
2025-01-05 -> ['769,817,855(again)', '379,173', '370(todo)']
33-
2025-01-04 -> ['079,212(todo),362,849', '369,311', '560,523', '53,210,207']
34-
2025-01-03 -> ['769,817,855(again)', '498(again),934', '370', '34,767', '444']
35-
2025-01-02 -> ['769,817,855(again)', '379,173', '815,871,593,1109', '1188,130,855(again)']
36+
2025-01-04 -> ['729,731', '079,212(todo),362,849', '369,311', '560,523', '53,210,207']
37+
2025-01-03 -> ['729,731', '769,817,855(again)', '498(again),934', '370', '34,767', '444']
38+
2025-01-02 -> ['729,731', '769,817,855(again)', '379,173', '815,871,593,1109', '1188,130,855(again)']
3639
2025-01-01 -> ['769,817,855(again)', '498(again),934', '079,212(todo),362,849']
3740
2024-12-31 -> ['498(again),934', '379,173']
3841
2024-12-30 -> ['498(again),934', '379,173', '079,212(todo),362,849', '369,311', '722,380']
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package LeetCodeJava.Array;
2+
3+
// https://leetcode.com/problems/my-calendar-ii/description/
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.TreeMap;
9+
10+
/**
11+
* 731. My Calendar II
12+
* Medium
13+
* Topics
14+
* Companies
15+
* Hint
16+
* You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a triple booking.
17+
*
18+
* A triple booking happens when three events have some non-empty intersection (i.e., some moment is common to all the three events.).
19+
*
20+
* The event can be represented as a pair of integers startTime and endTime that represents a booking on the half-open interval [startTime, endTime), the range of real numbers x such that startTime <= x < endTime.
21+
*
22+
* Implement the MyCalendarTwo class:
23+
*
24+
* MyCalendarTwo() Initializes the calendar object.
25+
* boolean book(int startTime, int endTime) Returns true if the event can be added to the calendar successfully without causing a triple booking. Otherwise, return false and do not add the event to the calendar.
26+
*
27+
*
28+
* Example 1:
29+
*
30+
* Input
31+
* ["MyCalendarTwo", "book", "book", "book", "book", "book", "book"]
32+
* [[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
33+
* Output
34+
* [null, true, true, true, false, true, true]
35+
*
36+
* Explanation
37+
* MyCalendarTwo myCalendarTwo = new MyCalendarTwo();
38+
* myCalendarTwo.book(10, 20); // return True, The event can be booked.
39+
* myCalendarTwo.book(50, 60); // return True, The event can be booked.
40+
* myCalendarTwo.book(10, 40); // return True, The event can be double booked.
41+
* myCalendarTwo.book(5, 15); // return False, The event cannot be booked, because it would result in a triple booking.
42+
* myCalendarTwo.book(5, 10); // return True, The event can be booked, as it does not use time 10 which is already double booked.
43+
* myCalendarTwo.book(25, 55); // return True, The event can be booked, as the time in [25, 40) will be double booked with the third event, the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.
44+
*
45+
*
46+
* Constraints:
47+
*
48+
* 0 <= start < end <= 109
49+
* At most 1000 calls will be made to book.
50+
* Seen this question in a real interview before?
51+
* 1/5
52+
* Yes
53+
* No
54+
* Accepted
55+
* 187.4K
56+
* Submissions
57+
* 298.6K
58+
* Acceptance Rate
59+
* 62.8%
60+
*/
61+
public class MyCalendar2 {
62+
63+
/**
64+
* Your MyCalendarTwo object will be instantiated and called as such:
65+
* MyCalendarTwo obj = new MyCalendarTwo();
66+
* boolean param_1 = obj.book(startTime,endTime);
67+
*/
68+
69+
// V0
70+
// class MyCalendarTwo {
71+
//
72+
// public MyCalendarTwo() {
73+
//
74+
// }
75+
//
76+
// public boolean book(int startTime, int endTime) {
77+
//
78+
// }
79+
// }
80+
81+
// V1-1
82+
// https://leetcode.com/problems/my-calendar-ii/editorial/
83+
// IDEA: Using Overlapped Intervals
84+
class MyCalendarTwo_1_1 {
85+
86+
private List<int[]> bookings;
87+
private List<int[]> overlapBookings;
88+
89+
// Return true if the booking [start1, end1) & [start2, end2) overlaps.
90+
private boolean doesOverlap(int start1, int end1, int start2, int end2) {
91+
return Math.max(start1, start2) < Math.min(end1, end2);
92+
}
93+
94+
// Return overlapping booking between [start1, end1) & [start2, end2).
95+
private int[] getOverlapped(int start1, int end1, int start2, int end2) {
96+
return new int[] { Math.max(start1, start2), Math.min(end1, end2) };
97+
}
98+
99+
public MyCalendarTwo_1_1() {
100+
bookings = new ArrayList<>();
101+
overlapBookings = new ArrayList<>();
102+
}
103+
104+
public boolean book(int start, int end) {
105+
// Returns false if the new booking has an overlap
106+
// with the existing double-booked bookings.
107+
for (int[] booking : overlapBookings) {
108+
if (doesOverlap(booking[0], booking[1], start, end)) {
109+
return false;
110+
}
111+
}
112+
113+
// Add the double overlapping bookings if any with the new booking.
114+
for (int[] booking : bookings) {
115+
if (doesOverlap(booking[0], booking[1], start, end)) {
116+
overlapBookings.add(
117+
getOverlapped(booking[0], booking[1], start, end));
118+
}
119+
}
120+
121+
// Add the new booking to the list of bookings.
122+
bookings.add(new int[] { start, end });
123+
return true;
124+
}
125+
}
126+
127+
128+
// V1-2
129+
// https://leetcode.com/problems/my-calendar-ii/editorial/
130+
// IDEA: Line Sweep
131+
class MyCalendarTwo_1_2 {
132+
133+
private TreeMap<Integer, Integer> bookingCount;
134+
private int maxOverlappedBooking;
135+
136+
public MyCalendarTwo_1_2() {
137+
bookingCount = new TreeMap<>();
138+
maxOverlappedBooking = 2;
139+
}
140+
141+
public boolean book(int start, int end) {
142+
// Increase the booking count at 'start' and decrease at 'end'.
143+
bookingCount.put(start, bookingCount.getOrDefault(start, 0) + 1);
144+
bookingCount.put(end, bookingCount.getOrDefault(end, 0) - 1);
145+
146+
int overlappedBooking = 0;
147+
148+
// Calculate the prefix sum of bookings.
149+
for (Map.Entry<Integer, Integer> entry : bookingCount.entrySet()) {
150+
overlappedBooking += entry.getValue();
151+
152+
// If the number of overlaps exceeds the allowed limit, rollback and
153+
// return false.
154+
if (overlappedBooking > maxOverlappedBooking) {
155+
// Rollback changes.
156+
bookingCount.put(start, bookingCount.get(start) - 1);
157+
bookingCount.put(end, bookingCount.get(end) + 1);
158+
159+
// Clean up if the count becomes zero to maintain the map clean.
160+
if (bookingCount.get(start) == 0) {
161+
bookingCount.remove(start);
162+
}
163+
164+
return false;
165+
}
166+
}
167+
168+
return true;
169+
}
170+
}
171+
172+
173+
// V2
174+
}

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,6 @@ public boolean book(int startTime, int endTime) {
14541454
* |----------| (old)
14551455
*
14561456
*
1457-
*
14581457
*/
14591458
// if ( endTime > existingStart || startTime < existingEnd ){
14601459
// return false;
@@ -1491,5 +1490,83 @@ private void sort(List<List<Integer>> bookedDays){
14911490

14921491
}
14931492

1493+
// LC 731
1494+
// https://leetcode.com/problems/my-calendar-ii/
1495+
// 2.53 - 3.30 PM
1496+
class MyCalendarTwo {
1497+
1498+
List<List<Integer>> booked;
1499+
Map<List<Integer>, Integer> overlapCnt;
1500+
1501+
public MyCalendarTwo() {
1502+
this.booked = new ArrayList<>();
1503+
this.overlapCnt = new HashMap<>();
1504+
}
1505+
1506+
public boolean book(int startTime, int endTime) {
1507+
1508+
List<Integer> tmp = new ArrayList<>();
1509+
tmp.add(startTime);
1510+
tmp.add(endTime);
1511+
1512+
// case 1) booked is empty
1513+
if(this.booked.isEmpty()){
1514+
this.booked.add(tmp);
1515+
return true;
1516+
}
1517+
1518+
boolean lessEqualsThreeOverlap = false;
1519+
1520+
1521+
for(List<Integer> x: this.booked){
1522+
/**
1523+
* |----|
1524+
* |------| (old)
1525+
*
1526+
* or
1527+
*
1528+
* |-----|
1529+
* |----| (old)
1530+
*
1531+
* or
1532+
*
1533+
* |---|
1534+
* |----------| (old)
1535+
*
1536+
*
1537+
*/
1538+
int existingStart = x.get(0);
1539+
int existingEnd = x.get(1);
1540+
1541+
if (startTime < existingEnd && existingStart < endTime) {
1542+
// case 2) has overlap, but `overlap count` <= 3
1543+
List<Integer> tmpExisting = new ArrayList<>();
1544+
tmpExisting.add(existingStart);
1545+
tmpExisting.add(existingEnd);
1546+
if(this.overlapCnt.get(tmpExisting) <= 3){
1547+
// update existing start, end
1548+
existingStart = Math.min(existingStart, startTime);
1549+
existingEnd = Math.max(existingEnd, endTime);
1550+
List<Integer> tmp2 = new ArrayList<>();
1551+
tmp2.add(existingStart);
1552+
tmp2.add(existingEnd);
1553+
this.overlapCnt.put(tmp2, this.overlapCnt.get(tmpExisting)+1); // update overlap cnt
1554+
this.overlapCnt.remove(tmpExisting);
1555+
return true;
1556+
}else{
1557+
// case 3) has overlap, and `overlap count` > 3
1558+
return false;
1559+
}
1560+
}
1561+
1562+
}
1563+
1564+
// case 4) no overlap
1565+
this.booked.add(tmp);
1566+
this.overlapCnt.put(tmp, 1); // update overlap cnt
1567+
return true;
1568+
}
1569+
}
1570+
14941571

14951572
}

0 commit comments

Comments
 (0)