Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 341a45e

Browse files
authored
feat: add solutions to lc problem: No.1257 (doocs#4136)
No.1257.Smallest Common Region
1 parent b8792d6 commit 341a45e

File tree

8 files changed

+352
-152
lines changed

8 files changed

+352
-152
lines changed

solution/1200-1299/1257.Smallest Common Region/README.md

Lines changed: 120 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ region2 = "New York"
7575

7676
<!-- solution:start -->
7777

78-
### 方法一
78+
### 方法一:哈希表
79+
80+
我们可以用一个哈希表 $\textit{g}$ 来存储每个区域的父区域,然后从 $\textit{region1}$ 开始,不断向上找到所有的父区域,直到根区域,将这些区域放入集合 $\textit{s}$ 中。然后从 $\textit{region2}$ 开始,不断向上找到第一个在 $\textit{s}$ 中的区域,即为最小公共区域。
81+
82+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为区域列表 $\textit{regions}$ 的长度。
7983

8084
<!-- tabs:start -->
8185

@@ -86,44 +90,43 @@ class Solution:
8690
def findSmallestRegion(
8791
self, regions: List[List[str]], region1: str, region2: str
8892
) -> str:
89-
m = {}
90-
for region in regions:
91-
for r in region[1:]:
92-
m[r] = region[0]
93+
g = {}
94+
for r in regions:
95+
x = r[0]
96+
for y in r[1:]:
97+
g[y] = x
9398
s = set()
94-
while m.get(region1):
95-
s.add(region1)
96-
region1 = m[region1]
97-
while m.get(region2):
98-
if region2 in s:
99-
return region2
100-
region2 = m[region2]
101-
return region1
99+
x = region1
100+
while x in g:
101+
s.add(x)
102+
x = g[x]
103+
x = region2
104+
while x in g and x not in s:
105+
x = g[x]
106+
return x
102107
```
103108

104109
#### Java
105110

106111
```java
107112
class Solution {
108113
public String findSmallestRegion(List<List<String>> regions, String region1, String region2) {
109-
Map<String, String> m = new HashMap<>();
110-
for (List<String> region : regions) {
111-
for (int i = 1; i < region.size(); ++i) {
112-
m.put(region.get(i), region.get(0));
114+
Map<String, String> g = new HashMap<>();
115+
for (var r : regions) {
116+
String x = r.get(0);
117+
for (String y : r.subList(1, r.size())) {
118+
g.put(y, x);
113119
}
114120
}
115121
Set<String> s = new HashSet<>();
116-
while (m.containsKey(region1)) {
117-
s.add(region1);
118-
region1 = m.get(region1);
122+
for (String x = region1; x != null; x = g.get(x)) {
123+
s.add(x);
119124
}
120-
while (m.containsKey(region2)) {
121-
if (s.contains(region2)) {
122-
return region2;
123-
}
124-
region2 = m.get(region2);
125+
String x = region2;
126+
while (g.get(x) != null && !s.contains(x)) {
127+
x = g.get(x);
125128
}
126-
return region1;
129+
return x;
127130
}
128131
}
129132
```
@@ -134,20 +137,22 @@ class Solution {
134137
class Solution {
135138
public:
136139
string findSmallestRegion(vector<vector<string>>& regions, string region1, string region2) {
137-
unordered_map<string, string> m;
138-
for (auto& region : regions)
139-
for (int i = 1; i < region.size(); ++i)
140-
m[region[i]] = region[0];
140+
unordered_map<string, string> g;
141+
for (const auto& r : regions) {
142+
string x = r[0];
143+
for (size_t i = 1; i < r.size(); ++i) {
144+
g[r[i]] = x;
145+
}
146+
}
141147
unordered_set<string> s;
142-
while (m.count(region1)) {
143-
s.insert(region1);
144-
region1 = m[region1];
148+
for (string x = region1; !x.empty(); x = g[x]) {
149+
s.insert(x);
145150
}
146-
while (m.count(region2)) {
147-
if (s.count(region2)) return region2;
148-
region2 = m[region2];
151+
string x = region2;
152+
while (!g[x].empty() && s.find(x) == s.end()) {
153+
x = g[x];
149154
}
150-
return region1;
155+
return x;
151156
}
152157
};
153158
```
@@ -156,24 +161,89 @@ public:
156161
157162
```go
158163
func findSmallestRegion(regions [][]string, region1 string, region2 string) string {
159-
m := make(map[string]string)
160-
for _, region := range regions {
161-
for i := 1; i < len(region); i++ {
162-
m[region[i]] = region[0]
164+
g := make(map[string]string)
165+
166+
for _, r := range regions {
167+
x := r[0]
168+
for _, y := range r[1:] {
169+
g[y] = x
163170
}
164171
}
172+
165173
s := make(map[string]bool)
166-
for region1 != "" {
167-
s[region1] = true
168-
region1 = m[region1]
174+
for x := region1; x != ""; x = g[x] {
175+
s[x] = true
169176
}
170-
for region2 != "" {
171-
if s[region2] {
172-
return region2
173-
}
174-
region2 = m[region2]
177+
178+
x := region2
179+
for g[x] != "" && !s[x] {
180+
x = g[x]
175181
}
176-
return region1
182+
183+
return x
184+
}
185+
```
186+
187+
#### TypeScript
188+
189+
```ts
190+
function findSmallestRegion(regions: string[][], region1: string, region2: string): string {
191+
const g: Record<string, string> = {};
192+
193+
for (const r of regions) {
194+
const x = r[0];
195+
for (const y of r.slice(1)) {
196+
g[y] = x;
197+
}
198+
}
199+
200+
const s: Set<string> = new Set();
201+
for (let x: string = region1; x !== undefined; x = g[x]) {
202+
s.add(x);
203+
}
204+
205+
let x: string = region2;
206+
while (g[x] !== undefined && !s.has(x)) {
207+
x = g[x];
208+
}
209+
210+
return x;
211+
}
212+
```
213+
214+
#### Rust
215+
216+
```rust
217+
use std::collections::{HashMap, HashSet};
218+
219+
impl Solution {
220+
pub fn find_smallest_region(regions: Vec<Vec<String>>, region1: String, region2: String) -> String {
221+
let mut g: HashMap<String, String> = HashMap::new();
222+
223+
for r in &regions {
224+
let x = &r[0];
225+
for y in &r[1..] {
226+
g.insert(y.clone(), x.clone());
227+
}
228+
}
229+
230+
let mut s: HashSet<String> = HashSet::new();
231+
let mut x = Some(region1);
232+
while let Some(region) = x {
233+
s.insert(region.clone());
234+
x = g.get(&region).cloned();
235+
}
236+
237+
let mut x = Some(region2);
238+
while let Some(region) = x {
239+
if s.contains(&region) {
240+
return region;
241+
}
242+
x = g.get(&region).cloned();
243+
}
244+
245+
String::new()
246+
}
177247
}
178248
```
179249

0 commit comments

Comments
 (0)