Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0729 (#3917)
Browse files Browse the repository at this point in the history
No.0729.My Calendar I
  • Loading branch information
yanglbme authored Jan 2, 2025
1 parent 01db11b commit 8380f3f
Show file tree
Hide file tree
Showing 9 changed files with 2,673 additions and 217 deletions.
967 changes: 895 additions & 72 deletions solution/0700-0799/0729.My Calendar I/README.md

Large diffs are not rendered by default.

959 changes: 888 additions & 71 deletions solution/0700-0799/0729.My Calendar I/README_EN.md

Large diffs are not rendered by default.

25 changes: 10 additions & 15 deletions solution/0700-0799/0729.My Calendar I/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
class MyCalendar {
public:
map<int, int> m;

MyCalendar() {
}

bool book(int start, int end) {
++m[start];
--m[end];
int s = 0;
for (auto& [k, v] : m) {
s += v;
if (s > 1) {
--m[start];
++m[end];
return false;
}
bool book(int startTime, int endTime) {
auto e = m.lower_bound(startTime + 1);
if (e != m.end() && e->second < endTime) {
return false;
}
m[endTime] = startTime;
return true;
}

private:
map<int, int> m;
};

/**
* Your MyCalendar object will be instantiated and called as such:
* MyCalendar* obj = new MyCalendar();
* bool param_1 = obj->book(start,end);
*/
* bool param_1 = obj->book(startTime,endTime);
*/
13 changes: 5 additions & 8 deletions solution/0700-0799/0729.My Calendar I/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@ func Constructor() MyCalendar {
}
}

func (this *MyCalendar) Book(start int, end int) bool {
if p, ok := this.rbt.Floor(start); ok && p.Value.(int) > start {
func (this *MyCalendar) Book(startTime int, endTime int) bool {
if p, ok := this.rbt.Ceiling(startTime + 1); ok && p.Value.(int) < endTime {
return false
}
if p, ok := this.rbt.Ceiling(start); ok && p.Key.(int) < end {
return false
}
this.rbt.Put(start, end)
this.rbt.Put(endTime, startTime)
return true
}

/**
* Your MyCalendar object will be instantiated and called as such:
* obj := Constructor();
* param_1 := obj.Book(start,end);
*/
* param_1 := obj.Book(startTime,endTime);
*/
23 changes: 8 additions & 15 deletions solution/0700-0799/0729.My Calendar I/Solution.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
import java.util.Map;
import java.util.TreeMap;

class MyCalendar {

private final TreeMap<Integer, Integer> tm = new TreeMap<>();

public MyCalendar() {
}

public boolean book(int start, int end) {
Map.Entry<Integer, Integer> ent = tm.floorEntry(start);
if (ent != null && ent.getValue() > start) {
return false;
}
ent = tm.ceilingEntry(start);
if (ent != null && ent.getKey() < end) {
public boolean book(int startTime, int endTime) {
var e = tm.ceilingEntry(startTime + 1);
if (e != null && e.getValue() < endTime) {
return false;
}
tm.put(start, end);
tm.put(endTime, startTime);
return true;
}
}

/**
* Your MyCalendar object will be instantiated and called as such: MyCalendar
* obj = new MyCalendar(); boolean param_1 = obj.book(start,end);
*/
* Your MyCalendar object will be instantiated and called as such:
* MyCalendar obj = new MyCalendar();
* boolean param_1 = obj.book(startTime,endTime);
*/
Loading

0 comments on commit 8380f3f

Please sign in to comment.