-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.0731
No.0731.My Calendar II
- Loading branch information
Showing
16 changed files
with
1,053 additions
and
547 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,29 @@ | ||
class MyCalendarTwo { | ||
public: | ||
map<int, int> m; | ||
|
||
MyCalendarTwo() { | ||
} | ||
|
||
bool book(int start, int end) { | ||
++m[start]; | ||
--m[end]; | ||
bool book(int startTime, int endTime) { | ||
++m[startTime]; | ||
--m[endTime]; | ||
int s = 0; | ||
for (auto& [_, v] : m) { | ||
s += v; | ||
if (s > 2) { | ||
--m[start]; | ||
++m[end]; | ||
--m[startTime]; | ||
++m[endTime]; | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private: | ||
map<int, int> m; | ||
}; | ||
|
||
/** | ||
* Your MyCalendarTwo object will be instantiated and called as such: | ||
* MyCalendarTwo* obj = new MyCalendarTwo(); | ||
* bool param_1 = obj->book(start,end); | ||
*/ | ||
* bool param_1 = obj->book(startTime,endTime); | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,34 @@ | ||
var MyCalendarTwo = function () { | ||
this.events = []; | ||
this.overlaps = []; | ||
this.tm = {}; | ||
}; | ||
|
||
/** | ||
* @param {number} start | ||
* @param {number} end | ||
* @param {number} startTime | ||
* @param {number} endTime | ||
* @return {boolean} | ||
*/ | ||
MyCalendarTwo.prototype.book = function (start, end) { | ||
for (let [s, e] of this.overlaps) { | ||
if (Math.max(start, s) < Math.min(end, e)) { | ||
return false; | ||
} | ||
} | ||
MyCalendarTwo.prototype.book = function (startTime, endTime) { | ||
this.tm[startTime] = (this.tm[startTime] || 0) + 1; | ||
this.tm[endTime] = (this.tm[endTime] || 0) - 1; | ||
let s = 0; | ||
|
||
for (let [s, e] of this.events) { | ||
if (Math.max(start, s) < Math.min(end, e)) { | ||
this.overlaps.push([Math.max(start, s), Math.min(end, e)]); | ||
for (const v of Object.values(this.tm)) { | ||
s += v; | ||
if (s > 2) { | ||
if (--this.tm[startTime] === 0) { | ||
delete this.tm[startTime]; | ||
} | ||
if (++this.tm[endTime] === 0) { | ||
delete this.tm[endTime]; | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
this.events.push([start, end]); | ||
return true; | ||
}; | ||
|
||
/** | ||
* Your MyCalendarTwo object will be instantiated and called as such: | ||
* var obj = new MyCalendarTwo() | ||
* var param_1 = obj.book(start,end) | ||
* var param_1 = obj.book(startTime,endTime) | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,30 @@ | ||
class MyCalendarTwo { | ||
private events: [number, number][]; | ||
private overlaps: [number, number][]; | ||
private tm: Record<number, number> = {}; | ||
|
||
constructor() { | ||
this.events = []; | ||
this.overlaps = []; | ||
} | ||
constructor() {} | ||
|
||
book(start: number, end: number): boolean { | ||
for (const [s, e] of this.overlaps) { | ||
if (Math.max(start, s) < Math.min(end, e)) { | ||
book(startTime: number, endTime: number): boolean { | ||
this.tm[startTime] = (this.tm[startTime] ?? 0) + 1; | ||
this.tm[endTime] = (this.tm[endTime] ?? 0) - 1; | ||
let s = 0; | ||
for (const v of Object.values(this.tm)) { | ||
s += v; | ||
if (s > 2) { | ||
if (--this.tm[startTime] === 0) { | ||
delete this.tm[startTime]; | ||
} | ||
if (++this.tm[endTime] === 0) { | ||
delete this.tm[endTime]; | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
for (const [s, e] of this.events) { | ||
if (Math.max(start, s) < Math.min(end, e)) { | ||
this.overlaps.push([Math.max(start, s), Math.min(end, e)]); | ||
} | ||
} | ||
|
||
this.events.push([start, end]); | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* Your MyCalendarTwo object will be instantiated and called as such: | ||
* var obj = new MyCalendarTwo() | ||
* var param_1 = obj.book(start,end) | ||
* var param_1 = obj.book(startTime,endTime) | ||
*/ |
Oops, something went wrong.