Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0731
Browse files Browse the repository at this point in the history
No.0731.My Calendar II
  • Loading branch information
yanglbme committed Jan 3, 2025
1 parent 8380f3f commit b9c048f
Show file tree
Hide file tree
Showing 16 changed files with 1,053 additions and 547 deletions.
519 changes: 339 additions & 180 deletions solution/0700-0799/0731.My Calendar II/README.md

Large diffs are not rendered by default.

537 changes: 358 additions & 179 deletions solution/0700-0799/0731.My Calendar II/README_EN.md

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions solution/0700-0799/0731.My Calendar II/Solution.cpp
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);
*/
34 changes: 18 additions & 16 deletions solution/0700-0799/0731.My Calendar II/Solution.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
type MyCalendarTwo struct {
*redblacktree.Tree
rbt *redblacktree.Tree[int, int]
}

func Constructor() MyCalendarTwo {
return MyCalendarTwo{redblacktree.NewWithIntComparator()}
return MyCalendarTwo{rbt: redblacktree.New[int, int]()}
}

func (this *MyCalendarTwo) Book(start int, end int) bool {
add := func(key, val int) {
if v, ok := this.Get(key); ok {
this.Put(key, v.(int)+val)
func (this *MyCalendarTwo) Book(startTime int, endTime int) bool {
merge := func(x, v int) {
c, _ := this.rbt.Get(x)
if c+v == 0 {
this.rbt.Remove(x)
} else {
this.Put(key, val)
this.rbt.Put(x, c+v)
}
}
add(start, 1)
add(end, -1)

merge(startTime, 1)
merge(endTime, -1)

s := 0
it := this.Iterator()
for it.Next() {
s += it.Value().(int)
for _, v := range this.rbt.Values() {
s += v
if s > 2 {
add(start, -1)
add(end, 1)
merge(startTime, -1)
merge(endTime, 1)
return false
}
}
Expand All @@ -32,5 +34,5 @@ func (this *MyCalendarTwo) Book(start int, end int) bool {
/**
* Your MyCalendarTwo object will be instantiated and called as such:
* obj := Constructor();
* param_1 := obj.Book(start,end);
*/
* param_1 := obj.Book(startTime,endTime);
*/
16 changes: 8 additions & 8 deletions solution/0700-0799/0731.My Calendar II/Solution.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
class MyCalendarTwo {
private Map<Integer, Integer> tm = new TreeMap<>();
private final Map<Integer, Integer> tm = new TreeMap<>();

public MyCalendarTwo() {
}

public boolean book(int start, int end) {
tm.put(start, tm.getOrDefault(start, 0) + 1);
tm.put(end, tm.getOrDefault(end, 0) - 1);
public boolean book(int startTime, int endTime) {
tm.merge(startTime, 1, Integer::sum);
tm.merge(endTime, -1, Integer::sum);
int s = 0;
for (int v : tm.values()) {
s += v;
if (s > 2) {
tm.put(start, tm.get(start) - 1);
tm.put(end, tm.get(end) + 1);
tm.merge(startTime, -1, Integer::sum);
tm.merge(endTime, 1, Integer::sum);
return false;
}
}
Expand All @@ -23,5 +23,5 @@ public boolean book(int start, int end) {
/**
* Your MyCalendarTwo object will be instantiated and called as such:
* MyCalendarTwo obj = new MyCalendarTwo();
* boolean param_1 = obj.book(start,end);
*/
* boolean param_1 = obj.book(startTime,endTime);
*/
34 changes: 18 additions & 16 deletions solution/0700-0799/0731.My Calendar II/Solution.js
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)
*/
12 changes: 6 additions & 6 deletions solution/0700-0799/0731.My Calendar II/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ class MyCalendarTwo:
def __init__(self):
self.sd = SortedDict()

def book(self, start: int, end: int) -> bool:
self.sd[start] = self.sd.get(start, 0) + 1
self.sd[end] = self.sd.get(end, 0) - 1
def book(self, startTime: int, endTime: int) -> bool:
self.sd[startTime] = self.sd.get(startTime, 0) + 1
self.sd[endTime] = self.sd.get(endTime, 0) - 1
s = 0
for v in self.sd.values():
s += v
if s > 2:
self.sd[start] -= 1
self.sd[end] += 1
self.sd[startTime] -= 1
self.sd[endTime] += 1
return False
return True


# Your MyCalendarTwo object will be instantiated and called as such:
# obj = MyCalendarTwo()
# param_1 = obj.book(start,end)
# param_1 = obj.book(startTime,endTime)
34 changes: 16 additions & 18 deletions solution/0700-0799/0731.My Calendar II/Solution.ts
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)
*/
Loading

0 comments on commit b9c048f

Please sign in to comment.