Skip to content

Commit 3538a58

Browse files
author
Partho Biswas
committed
729. My Calendar I
1 parent ba7c8c7 commit 3538a58

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ Check this [golden](https://tinyurl.com/ujopecz) post.
376376
|08| [145. Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)| [Python](https://tinyurl.com/wu6rdaw/145_Binary_Tree_Postorder_Traversal.py)| --- | Hard | Fundamentals |
377377
|09| [108. Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)| [Python](https://tinyurl.com/wu6rdaw/108_Convert_Sorted_Array_to_Binary_Search_Tree.py)| --- | Easy | Fundamentals |
378378
|10| [109. Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/)| [Python](https://tinyurl.com/wu6rdaw/109_Convert_Sorted_List_to_Binary_Search_Tree.py)| --- | Medium | Classic problem. Very important |
379+
|11| **[729. My Calendar I](https://tinyurl.com/r3ew2lb)** | [Python](https://tinyurl.com/wu6rdaw/729_My_Calendar_I.py), [Swift](https://tinyurl.com/wuja3c4/729_My_Calendar_I.swift)| [Art 1](https://tinyurl.com/tbd2z7u) | Medium | Use self balancing BST for O(nlogn) solution |
379380

380381
</p>
381382
</details>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class TreeNode:
2+
def __init__(self, start, end):
3+
self.start = start
4+
self.end = end
5+
self.left = None
6+
self.right = None
7+
8+
class MyCalendar(object):
9+
10+
def __init__(self):
11+
self.root = None
12+
13+
def bookHelper(self, start, end, node):
14+
if start >= node.end:
15+
if node.right:
16+
return self.bookHelper(start, end, node.right)
17+
else:
18+
node.right = TreeNode(start, end)
19+
return True
20+
elif end <= node.start:
21+
if node.left:
22+
return self.bookHelper(start, end, node.left)
23+
else:
24+
node.left = TreeNode(start, end)
25+
return True
26+
else:
27+
return False
28+
29+
def book(self, start, end):
30+
"""
31+
:type start: int
32+
:type end: int
33+
:rtype: bool
34+
"""
35+
if not self.root:
36+
self.root = TreeNode(start, end)
37+
return True
38+
return self.bookHelper(start, end, self.root)
39+
40+
# Your MyCalendar object will be instantiated and called as such:
41+
# obj = MyCalendar()
42+
# param_1 = obj.book(start,end)

leetcode.com/swift/DS_ALGO_PRAC.playground/Sources/swift/362_Design_Hit_Counter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class HitCounter {
2828
var hitCount = 0
2929
var startIdx = 0
3030
for i in stride(from: self.timestamps.count - 1, to: -1, by: -1) {
31-
var startTime = self.timestamps[i]
31+
let startTime = self.timestamps[i]
3232
if timestamp - startTime < 300 {
3333
hitCount += self.timestampHitMap[startTime] ?? 0
3434
startIdx = i
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import Foundation
2+
3+
class TreeNode {
4+
let start: Int
5+
let end: Int
6+
var left: TreeNode?
7+
var right: TreeNode?
8+
9+
init(_ start: Int, _ end: Int) {
10+
self.start = start
11+
self.end = end
12+
}
13+
}
14+
15+
class MyCalendar {
16+
17+
var root: TreeNode?
18+
19+
init() {
20+
21+
}
22+
23+
func bookHelper(_ start: Int, _ end: Int, _ node: inout TreeNode) -> Bool {
24+
if start >= node.end {
25+
if node.right != nil {
26+
return self.bookHelper(start, end, &node.right!)
27+
} else {
28+
node.right = TreeNode(start, end)
29+
return true
30+
}
31+
} else if end <= node.start {
32+
if node.left != nil {
33+
return self.bookHelper(start, end, &node.left!)
34+
} else {
35+
node.left = TreeNode(start, end)
36+
return true
37+
}
38+
} else {
39+
return false
40+
}
41+
}
42+
43+
func book(_ start: Int, _ end: Int) -> Bool {
44+
if let root = self.root {
45+
return self.bookHelper(start, end, &self.root!)
46+
}
47+
self.root = TreeNode(start, end)
48+
return true
49+
}
50+
}
51+
52+
/**
53+
* Your MyCalendar object will be instantiated and called as such:
54+
* let obj = MyCalendar()
55+
* let ret_1: Bool = obj.book(start, end)
56+
*/

0 commit comments

Comments
 (0)