diff --git a/solution/0700-0799/0731.My Calendar II/README.md b/solution/0700-0799/0731.My Calendar II/README.md index 1971961290950..04d8becf7d485 100644 --- a/solution/0700-0799/0731.My Calendar II/README.md +++ b/solution/0700-0799/0731.My Calendar II/README.md @@ -208,6 +208,43 @@ func (this *MyCalendarTwo) Book(start int, end int) bool { */ ``` +#### JavaScript + +```js +var MyCalendarTwo = function () { + this.events = []; + this.overlaps = []; +}; + +/** + * @param {number} start + * @param {number} end + * @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; + } + } + + 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)]); + } + } + + 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) + */ +``` + diff --git a/solution/0700-0799/0731.My Calendar II/README_EN.md b/solution/0700-0799/0731.My Calendar II/README_EN.md index 5d3abb9bcf791..b3f664980ff95 100644 --- a/solution/0700-0799/0731.My Calendar II/README_EN.md +++ b/solution/0700-0799/0731.My Calendar II/README_EN.md @@ -206,6 +206,43 @@ func (this *MyCalendarTwo) Book(start int, end int) bool { */ ``` +#### JavaScript + +```js +var MyCalendarTwo = function () { + this.events = []; + this.overlaps = []; +}; + +/** + * @param {number} start + * @param {number} end + * @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; + } + } + + 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)]); + } + } + + 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) + */ +``` + diff --git a/solution/0700-0799/0731.My Calendar II/Solution.js b/solution/0700-0799/0731.My Calendar II/Solution.js new file mode 100644 index 0000000000000..53cfad370e6f7 --- /dev/null +++ b/solution/0700-0799/0731.My Calendar II/Solution.js @@ -0,0 +1,32 @@ +var MyCalendarTwo = function () { + this.events = []; + this.overlaps = []; +}; + +/** + * @param {number} start + * @param {number} end + * @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; + } + } + + 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)]); + } + } + + 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) + */