Skip to content

feat: add solutions to lc problem: No.1257 #4136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 120 additions & 50 deletions solution/1200-1299/1257.Smallest Common Region/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ region2 = "New York"

<!-- solution:start -->

### 方法一
### 方法一:哈希表

我们可以用一个哈希表 $\textit{g}$ 来存储每个区域的父区域,然后从 $\textit{region1}$ 开始,不断向上找到所有的父区域,直到根区域,将这些区域放入集合 $\textit{s}$ 中。然后从 $\textit{region2}$ 开始,不断向上找到第一个在 $\textit{s}$ 中的区域,即为最小公共区域。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为区域列表 $\textit{regions}$ 的长度。

<!-- tabs:start -->

Expand All @@ -86,44 +90,43 @@ class Solution:
def findSmallestRegion(
self, regions: List[List[str]], region1: str, region2: str
) -> str:
m = {}
for region in regions:
for r in region[1:]:
m[r] = region[0]
g = {}
for r in regions:
x = r[0]
for y in r[1:]:
g[y] = x
s = set()
while m.get(region1):
s.add(region1)
region1 = m[region1]
while m.get(region2):
if region2 in s:
return region2
region2 = m[region2]
return region1
x = region1
while x in g:
s.add(x)
x = g[x]
x = region2
while x in g and x not in s:
x = g[x]
return x
```

#### Java

```java
class Solution {
public String findSmallestRegion(List<List<String>> regions, String region1, String region2) {
Map<String, String> m = new HashMap<>();
for (List<String> region : regions) {
for (int i = 1; i < region.size(); ++i) {
m.put(region.get(i), region.get(0));
Map<String, String> g = new HashMap<>();
for (var r : regions) {
String x = r.get(0);
for (String y : r.subList(1, r.size())) {
g.put(y, x);
}
}
Set<String> s = new HashSet<>();
while (m.containsKey(region1)) {
s.add(region1);
region1 = m.get(region1);
for (String x = region1; x != null; x = g.get(x)) {
s.add(x);
}
while (m.containsKey(region2)) {
if (s.contains(region2)) {
return region2;
}
region2 = m.get(region2);
String x = region2;
while (g.get(x) != null && !s.contains(x)) {
x = g.get(x);
}
return region1;
return x;
}
}
```
Expand All @@ -134,20 +137,22 @@ class Solution {
class Solution {
public:
string findSmallestRegion(vector<vector<string>>& regions, string region1, string region2) {
unordered_map<string, string> m;
for (auto& region : regions)
for (int i = 1; i < region.size(); ++i)
m[region[i]] = region[0];
unordered_map<string, string> g;
for (const auto& r : regions) {
string x = r[0];
for (size_t i = 1; i < r.size(); ++i) {
g[r[i]] = x;
}
}
unordered_set<string> s;
while (m.count(region1)) {
s.insert(region1);
region1 = m[region1];
for (string x = region1; !x.empty(); x = g[x]) {
s.insert(x);
}
while (m.count(region2)) {
if (s.count(region2)) return region2;
region2 = m[region2];
string x = region2;
while (!g[x].empty() && s.find(x) == s.end()) {
x = g[x];
}
return region1;
return x;
}
};
```
Expand All @@ -156,24 +161,89 @@ public:

```go
func findSmallestRegion(regions [][]string, region1 string, region2 string) string {
m := make(map[string]string)
for _, region := range regions {
for i := 1; i < len(region); i++ {
m[region[i]] = region[0]
g := make(map[string]string)

for _, r := range regions {
x := r[0]
for _, y := range r[1:] {
g[y] = x
}
}

s := make(map[string]bool)
for region1 != "" {
s[region1] = true
region1 = m[region1]
for x := region1; x != ""; x = g[x] {
s[x] = true
}
for region2 != "" {
if s[region2] {
return region2
}
region2 = m[region2]

x := region2
for g[x] != "" && !s[x] {
x = g[x]
}
return region1

return x
}
```

#### TypeScript

```ts
function findSmallestRegion(regions: string[][], region1: string, region2: string): string {
const g: Record<string, string> = {};

for (const r of regions) {
const x = r[0];
for (const y of r.slice(1)) {
g[y] = x;
}
}

const s: Set<string> = new Set();
for (let x: string = region1; x !== undefined; x = g[x]) {
s.add(x);
}

let x: string = region2;
while (g[x] !== undefined && !s.has(x)) {
x = g[x];
}

return x;
}
```

#### Rust

```rust
use std::collections::{HashMap, HashSet};

impl Solution {
pub fn find_smallest_region(regions: Vec<Vec<String>>, region1: String, region2: String) -> String {
let mut g: HashMap<String, String> = HashMap::new();

for r in &regions {
let x = &r[0];
for y in &r[1..] {
g.insert(y.clone(), x.clone());
}
}

let mut s: HashSet<String> = HashSet::new();
let mut x = Some(region1);
while let Some(region) = x {
s.insert(region.clone());
x = g.get(&region).cloned();
}

let mut x = Some(region2);
while let Some(region) = x {
if s.contains(&region) {
return region;
}
x = g.get(&region).cloned();
}

String::new()
}
}
```

Expand Down
Loading
Loading