diff --git a/solution/0700-0799/0731.My Calendar II/README.md b/solution/0700-0799/0731.My Calendar II/README.md index 04d8becf7d485..60195c75c0c02 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 { */ ``` +#### TypeScript + +```ts +class MyCalendarTwo { + private events: [number, number][]; + private overlaps: [number, number][]; + + constructor() { + this.events = []; + this.overlaps = []; + } + + book(start: number, end: number): boolean { + for (const [s, e] of this.overlaps) { + if (Math.max(start, s) < Math.min(end, e)) { + 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) + */ +``` + #### JavaScript ```js 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 b3f664980ff95..4cc78f852a9b6 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 { */ ``` +#### TypeScript + +```ts +class MyCalendarTwo { + private events: [number, number][]; + private overlaps: [number, number][]; + + constructor() { + this.events = []; + this.overlaps = []; + } + + book(start: number, end: number): boolean { + for (const [s, e] of this.overlaps) { + if (Math.max(start, s) < Math.min(end, e)) { + 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) + */ +``` + #### JavaScript ```js diff --git a/solution/0700-0799/0731.My Calendar II/Solution.ts b/solution/0700-0799/0731.My Calendar II/Solution.ts new file mode 100644 index 0000000000000..28dba25de72ab --- /dev/null +++ b/solution/0700-0799/0731.My Calendar II/Solution.ts @@ -0,0 +1,32 @@ +class MyCalendarTwo { + private events: [number, number][]; + private overlaps: [number, number][]; + + constructor() { + this.events = []; + this.overlaps = []; + } + + book(start: number, end: number): boolean { + for (const [s, e] of this.overlaps) { + if (Math.max(start, s) < Math.min(end, e)) { + 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) + */