You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0700-0799/0729.My Calendar I/README_EN.md
+7-1
Original file line number
Diff line number
Diff line change
@@ -63,7 +63,13 @@ myCalendar.book(20, 30); // return True, The event can be booked, as the first e
63
63
64
64
<!-- solution:start -->
65
65
66
-
### Solution 1
66
+
### Solution 1: Ordered Set
67
+
68
+
We can use an ordered set to store the schedule. An ordered set can perform insert, delete, and search operations in $O(\log n)$ time. The elements in the ordered set are sorted by the $\textit{endTime}$ of the schedule in ascending order.
69
+
70
+
When calling the $\text{book}(start, end)$ method, we search for the first schedule in the ordered set with an end time greater than $\textit{start}$. If it exists and its start time is less than $\textit{end}$, it means there is a double booking, and we return $\text{false}$. Otherwise, we insert $\textit{end}$ as the key and $\textit{start}$ as the value into the ordered set and return $\text{true}$.
71
+
72
+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the number of schedules.
Copy file name to clipboardexpand all lines: solution/0700-0799/0734.Sentence Similarity/README_EN.md
+124-22
Original file line number
Diff line number
Diff line change
@@ -78,7 +78,17 @@ tags:
78
78
79
79
<!-- solution:start -->
80
80
81
-
### Solution 1
81
+
### Solution 1: Hash Table
82
+
83
+
First, we check if the lengths of $\textit{sentence1}$ and $\textit{sentence2}$ are equal. If they are not equal, return $\text{false}$.
84
+
85
+
Then we use a hash table $\textit{s}$ to store all similar word pairs. For each word pair $[x, y]$ in $\textit{similarPairs}$, we add $x$ and $y$ to the hash table $\textit{s}$.
86
+
87
+
Next, we traverse $\textit{sentence1}$ and $\textit{sentence2}$. For each position $i$, if $\textit{sentence1}[i]$ is not equal to $\textit{sentence2}[i]$, and $(\textit{sentence1}[i], \textit{sentence2}[i])$ and $(\textit{sentence2}[i], \textit{sentence1}[i])$ are not in the hash table $\textit{s}$, then return $\text{false}$.
88
+
89
+
If the traversal ends without returning $\text{false}$, it means $\textit{sentence1}$ and $\textit{sentence2}$ are similar, so return $\text{true}$.
90
+
91
+
The time complexity is $O(L)$, and the space complexity is $O(L)$, where $L$ is the sum of the lengths of all strings in the problem.
82
92
83
93
<!-- tabs:start -->
84
94
@@ -91,10 +101,11 @@ class Solution:
91
101
) -> bool:
92
102
iflen(sentence1) !=len(sentence2):
93
103
returnFalse
94
-
s = {(a, b) for a, b in similarPairs}
95
-
returnall(
96
-
a == b or (a, b) in s or (b, a) in s for a, b inzip(sentence1, sentence2)
97
-
)
104
+
s = {(x, y) for x, y in similarPairs}
105
+
for x, y inzip(sentence1, sentence2):
106
+
if x != y and (x, y) notin s and (y, x) notin s:
107
+
returnFalse
108
+
returnTrue
98
109
```
99
110
100
111
#### Java
@@ -106,13 +117,14 @@ class Solution {
106
117
if (sentence1.length != sentence2.length) {
107
118
returnfalse;
108
119
}
109
-
Set<String> s =newHashSet<>();
110
-
for (List<String> e: similarPairs) {
111
-
s.add(e.get(0) +"."+ e.get(1));
120
+
Set<List<String>> s =newHashSet<>();
121
+
for (var p: similarPairs) {
122
+
s.add(p);
112
123
}
113
-
for (int i =0; i < sentence1.length; ++i) {
114
-
String a = sentence1[i], b = sentence2[i];
115
-
if (!a.equals(b) &&!s.contains(a +"."+ b) &&!s.contains(b +"."+ a)) {
0 commit comments