Skip to content

Commit d10b113

Browse files
committed
leetcode
1 parent 28fa373 commit d10b113

File tree

4 files changed

+382
-0
lines changed

4 files changed

+382
-0
lines changed

MyCalendar-III/my_calendar_III.dart

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
3+
-* My Calendar III *-
4+
5+
6+
A k-booking happens when k events have some non-empty intersection (i.e., there is some time that is common to all k events.)
7+
8+
You are given some events [start, end), after each given event, return an integer k representing the maximum k-booking between all the previous events.
9+
10+
Implement the MyCalendarThree class:
11+
12+
MyCalendarThree() Initializes the object.
13+
int book(int start, int end) Returns an integer k representing the largest integer such that there exists a k-booking in the calendar.
14+
15+
16+
Example 1:
17+
18+
Input
19+
["MyCalendarThree", "book", "book", "book", "book", "book", "book"]
20+
[[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
21+
Output
22+
[null, 1, 1, 2, 3, 3, 3]
23+
24+
Explanation
25+
MyCalendarThree myCalendarThree = new MyCalendarThree();
26+
myCalendarThree.book(10, 20); // return 1, The first event can be booked and is disjoint, so the maximum k-booking is a 1-booking.
27+
myCalendarThree.book(50, 60); // return 1, The second event can be booked and is disjoint, so the maximum k-booking is a 1-booking.
28+
myCalendarThree.book(10, 40); // return 2, The third event [10, 40) intersects the first event, and the maximum k-booking is a 2-booking.
29+
myCalendarThree.book(5, 15); // return 3, The remaining events cause the maximum K-booking to be only a 3-booking.
30+
myCalendarThree.book(5, 10); // return 3
31+
myCalendarThree.book(25, 55); // return 3
32+
33+
34+
Constraints:
35+
36+
0 <= start < end <= 109
37+
At most 400 calls will be made to book.
38+
39+
*/
40+
import 'dart:math';
41+
42+
class A {
43+
// Runtime: 1526 ms, faster than 100.00% of Dart online submissions for My Calendar III.
44+
// Memory Usage: 174.4 MB, less than 100.00% of Dart online submissions for My Calendar III.
45+
late List<int> bookingStart;
46+
late List<int> bookingEnd;
47+
A() {
48+
this.bookingStart = [];
49+
this.bookingEnd = [];
50+
}
51+
52+
int book(int start, int end) {
53+
// we will start adding he value inside the list start to end
54+
bookingStart.add(start);
55+
bookingEnd.add(end);
56+
// than we will sort out all the booking to arrange them
57+
bookingStart.sort();
58+
bookingEnd.sort();
59+
// index of the booking
60+
int i = 0;
61+
// count the amount of bookings we got
62+
int count = 0;
63+
// length of the booking list
64+
int n = bookingStart.length;
65+
// represent the second value of the booking
66+
int j = 0;
67+
// holds the max integer value
68+
int result = 0;
69+
// assuming that the our index value is not less than the whole length means it's not empty
70+
while (i < n) {
71+
// while the booking start is greater than the at the point of booking ending
72+
while (bookingStart[i] >= bookingEnd[j]) {
73+
// we will increment the value
74+
j++;
75+
// while keep track of the booking
76+
count--;
77+
}
78+
i++;
79+
count++;
80+
// than we will get the max integer
81+
result = max(result, count);
82+
}
83+
return result;
84+
}
85+
}
86+
87+
extension DefaultMap<K, V> on Map<K, V> {
88+
V getOrElse(K key, V defaultValue) {
89+
if (this.containsKey(key)) {
90+
return this[keys]!;
91+
} else {
92+
return defaultValue;
93+
}
94+
}
95+
}
96+
97+
class MyCalendarThree {
98+
// Runtime: 1635 ms, faster than 100.00% of Dart online submissions for My Calendar III.
99+
// Memory Usage: 164.3 MB, less than 100.00% of Dart online submissions for My Calendar III.
100+
101+
late List<int> starting;
102+
late List<int> ending;
103+
MyCalendarThree() {
104+
starting = [];
105+
ending = [];
106+
}
107+
108+
int book(int start, int end) {
109+
starting.add(start);
110+
ending.add(end);
111+
starting.sort();
112+
ending.sort();
113+
114+
int i = 0;
115+
int j = 0;
116+
int ans = 0;
117+
int temp = 0;
118+
while (i < starting.length && j < ending.length) {
119+
if (i < starting.length &&
120+
j < ending.length &&
121+
starting.elementAt(i) < ending.elementAt(j)) {
122+
temp++;
123+
i++;
124+
ans = max(temp, ans);
125+
} else {
126+
temp--;
127+
j++;
128+
}
129+
}
130+
return ans;
131+
}
132+
}

MyCalendar-III/my_calendar_III.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package main
2+
3+
import "math"
4+
5+
type MyCalendarThree struct {
6+
vals map[int]int
7+
lazy map[int]int
8+
}
9+
10+
func Constructor() MyCalendarThree {
11+
vals := make(map[int]int)
12+
lazy := make(map[int]int)
13+
14+
return MyCalendarThree{
15+
vals: vals,
16+
lazy: lazy,
17+
}
18+
}
19+
20+
func (this *MyCalendarThree) Book(start int, end int) int {
21+
this.Update(start, end-1, 0, int(math.Pow(10, 9)), 1)
22+
return this.vals[1]
23+
}
24+
25+
func (this *MyCalendarThree) Update(start int, end int, left int, right int, idx int) {
26+
if start > right || end < left {
27+
return
28+
}
29+
30+
if start <= left && right <= end {
31+
this.vals[idx]++
32+
this.lazy[idx]++
33+
} else {
34+
mid := (right-left)/2 + left
35+
this.Update(start, end, left, mid, idx*2)
36+
this.Update(start, end, mid+1, right, idx*2+1)
37+
this.vals[idx] = this.lazy[idx] + max(this.vals[2*idx], this.vals[2*idx+1])
38+
}
39+
}
40+
41+
func max(x, y int) int {
42+
if x > y {
43+
return x
44+
}
45+
return y
46+
}
47+
48+
/*
49+
50+
type indexed = struct {
51+
idx int32
52+
val int32
53+
}
54+
type MyCalendarThree []indexed
55+
56+
func Constructor() MyCalendarThree {
57+
return MyCalendarThree{}
58+
}
59+
60+
func appendSorted2(sli []indexed, former, later indexed) []indexed {
61+
var f = func(item indexed) func(int) bool {
62+
return func(i int) bool {
63+
if sli[i].idx == item.idx {
64+
return sli[i].val >= item.val
65+
}
66+
return sli[i].idx >= item.idx
67+
}
68+
}
69+
iF, iL := sort.Search(len(sli), f(former)), sort.Search(len(sli), f(later))
70+
if iF == len(sli) {
71+
return append(sli, former, later)
72+
}
73+
if iL == len(sli) {
74+
sli = append(sli, later, indexed{})
75+
copy(sli[iF+1:], sli[iF:])
76+
sli[iF] = former
77+
return sli
78+
}
79+
sli = append(sli, indexed{}, indexed{})
80+
copy(sli[iL+2:], sli[iL:])
81+
if iF != iL {
82+
copy(sli[iF+1:iL+1], sli[iF:iL])
83+
}
84+
sli[iF], sli[iL+1] = former, later
85+
return sli
86+
}
87+
88+
func (c *MyCalendarThree) Book(start, end int) (max int) {
89+
*c = appendSorted2(*c, indexed{int32(start), 1}, indexed{int32(end), -1})
90+
var curr int
91+
for _, v := range *c {
92+
curr += int(v.val)
93+
if curr > max {
94+
max = curr
95+
}
96+
}
97+
return
98+
}
99+
100+
101+
*/

MyCalendar-III/my_calendar_III.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# 🔥 My Calendar III 🔥 || 3 Solutions || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
```dart
6+
class MyCalendarThree {
7+
// Runtime: 1526 ms, faster than 100.00% of Dart online submissions for My Calendar III.
8+
// Memory Usage: 174.4 MB, less than 100.00% of Dart online submissions for My Calendar III.
9+
late List<int> bookingStart;
10+
late List<int> bookingEnd;
11+
MyCalendarThree() {
12+
this.bookingStart = [];
13+
this.bookingEnd = [];
14+
}
15+
16+
int book(int start, int end) {
17+
// we will start adding he value inside the list start to end
18+
bookingStart.add(start);
19+
bookingEnd.add(end);
20+
// than we will sort out all the booking to arrange them
21+
bookingStart.sort();
22+
bookingEnd.sort();
23+
// index of the booking
24+
int i = 0;
25+
// count the amount of bookings we got
26+
int count = 0;
27+
// length of the booking list
28+
int n = bookingStart.length;
29+
// represent the second value of the booking
30+
int j = 0;
31+
// holds the max integer value
32+
int result = 0;
33+
// assuming that the our index value is not less than the whole length means it's not empty
34+
while (i < n) {
35+
// while the booking start is greater than the at the point of booking ending
36+
while (bookingStart[i] >= bookingEnd[j]) {
37+
// we will increment the value
38+
j++;
39+
// while keep track of the booking
40+
count--;
41+
}
42+
i++;
43+
count++;
44+
// than we will get the max integer
45+
result = max(result, count);
46+
}
47+
return result;
48+
}
49+
}
50+
```
51+
52+
## Solution - 2
53+
54+
```dart
55+
class MyCalendarThree {
56+
// Runtime: 1635 ms, faster than 100.00% of Dart online submissions for My Calendar III.
57+
// Memory Usage: 164.3 MB, less than 100.00% of Dart online submissions for My Calendar III.
58+
59+
late List<int> starting;
60+
late List<int> ending;
61+
MyCalendarThree() {
62+
starting = [];
63+
ending = [];
64+
}
65+
66+
int book(int start, int end) {
67+
starting.add(start);
68+
ending.add(end);
69+
starting.sort();
70+
ending.sort();
71+
72+
int i = 0;
73+
int j = 0;
74+
int ans = 0;
75+
int temp = 0;
76+
while (i < starting.length && j < ending.length) {
77+
if (i < starting.length &&
78+
j < ending.length &&
79+
starting.elementAt(i) < ending.elementAt(j)) {
80+
temp++;
81+
i++;
82+
ans = max(temp, ans);
83+
} else {
84+
temp--;
85+
j++;
86+
}
87+
}
88+
return ans;
89+
}
90+
}
91+
```
92+
93+
## Solution - 3
94+
95+
```go
96+
// Runtime: 45 ms, faster than 100.00% of Go online submissions for My Calendar III.
97+
// Memory Usage: 7.2 MB, less than 100.00% of Go online submissions for My Calendar III.
98+
99+
type indexed = struct {
100+
idx int32
101+
val int32
102+
}
103+
type MyCalendarThree []indexed
104+
105+
func Constructor() MyCalendarThree {
106+
return MyCalendarThree{}
107+
}
108+
109+
func appendSorted2(sli []indexed, former, later indexed) []indexed {
110+
var f = func(item indexed) func(int) bool {
111+
return func(i int) bool {
112+
if sli[i].idx == item.idx {
113+
return sli[i].val >= item.val
114+
}
115+
return sli[i].idx >= item.idx
116+
}
117+
}
118+
iF, iL := sort.Search(len(sli), f(former)), sort.Search(len(sli), f(later))
119+
if iF == len(sli) {
120+
return append(sli, former, later)
121+
}
122+
if iL == len(sli) {
123+
sli = append(sli, later, indexed{})
124+
copy(sli[iF+1:], sli[iF:])
125+
sli[iF] = former
126+
return sli
127+
}
128+
sli = append(sli, indexed{}, indexed{})
129+
copy(sli[iL+2:], sli[iL:])
130+
if iF != iL {
131+
copy(sli[iF+1:iL+1], sli[iF:iL])
132+
}
133+
sli[iF], sli[iL+1] = former, later
134+
return sli
135+
}
136+
137+
func (c *MyCalendarThree) Book(start, end int) (max int) {
138+
*c = appendSorted2(*c, indexed{int32(start), 1}, indexed{int32(end), -1})
139+
var current int
140+
for _, v := range *c {
141+
current += int(v.val)
142+
if current > max {
143+
max = current
144+
}
145+
}
146+
return
147+
}
148+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
7474
- [Isomorphic Strings](IsomorphicStrings/isomorphic_strings.dart)
7575
- [Add One Row to Tree](AddOneRowToTree/add_one_row_to_tree.dart)
7676
- [Time Based Key-Value Store](TimeBasedKeyValueStore/time_based_key_value_store.dart)
77+
- [My Calendar III](MyCalendar-III/my_calendar_III.dart)
7778

7879
## Reach me via
7980

0 commit comments

Comments
 (0)