Skip to content

Commit 1becffc

Browse files
committed
feat: add solutions to lc problem: No.2077
No.2077.Paths in Maze That Lead to Same Room
1 parent 4571842 commit 1becffc

File tree

6 files changed

+301
-2
lines changed

6 files changed

+301
-2
lines changed

solution/2000-2099/2077.Paths in Maze That Lead to Same Room/README.md

+110-1
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,131 @@ There are no cycles of length 3.
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:哈希表**
60+
61+
长度为 `3` 的环,由三个顶点、三条边组成。我们假设三个顶点分别为 `a`, `b`, `c`
62+
63+
那么一定存在 `c <=> a``c <=> b` 以及 `a <=> b`,即环中的每个点都与其他两点直接相连。我们可以用哈希表来存放每个点的相邻点。
64+
65+
由于环的长度为 `3`,每个相同的环会被重复统计 `3` 次,因此答案需除以 `3`
66+
67+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。
68+
5969
<!-- tabs:start -->
6070

6171
### **Python3**
6272

6373
<!-- 这里可写当前语言的特殊实现逻辑 -->
6474

6575
```python
66-
76+
class Solution:
77+
def numberOfPaths(self, n: int, corridors: List[List[int]]) -> int:
78+
g = defaultdict(set)
79+
for a, b in corridors:
80+
g[a].add(b)
81+
g[b].add(a)
82+
ans = 0
83+
for i in range(1, n + 1):
84+
for j, k in combinations(g[i], 2):
85+
if j in g[k]:
86+
ans += 1
87+
return ans // 3
6788
```
6889

6990
### **Java**
7091

7192
<!-- 这里可写当前语言的特殊实现逻辑 -->
7293

7394
```java
95+
class Solution {
96+
public int numberOfPaths(int n, int[][] corridors) {
97+
Set<Integer>[] g = new Set[n + 1];
98+
for (int i = 0; i <= n; ++i) {
99+
g[i] = new HashSet<>();
100+
}
101+
for (var c : corridors) {
102+
int a = c[0], b = c[1];
103+
g[a].add(b);
104+
g[b].add(a);
105+
}
106+
int ans = 0;
107+
for (int c = 1; c <= n; ++c) {
108+
var nxt = new ArrayList<>(g[c]);
109+
int m = nxt.size();
110+
for (int i = 0; i < m; ++i) {
111+
for (int j = i + 1; j < m; ++j) {
112+
int a = nxt.get(i), b = nxt.get(j);
113+
if (g[b].contains(a)) {
114+
++ans;
115+
}
116+
}
117+
}
118+
}
119+
return ans / 3;
120+
}
121+
}
122+
```
123+
124+
### **C++**
125+
126+
```cpp
127+
class Solution {
128+
public:
129+
int numberOfPaths(int n, vector<vector<int>>& corridors) {
130+
vector<unordered_set<int>> g(n + 1);
131+
for (auto& c : corridors) {
132+
int a = c[0], b = c[1];
133+
g[a].insert(b);
134+
g[b].insert(a);
135+
}
136+
int ans = 0;
137+
for (int c = 1; c <= n; ++c) {
138+
vector<int> nxt;
139+
nxt.assign(g[c].begin(), g[c].end());
140+
int m = nxt.size();
141+
for (int i = 0; i < m; ++i) {
142+
for (int j = i + 1; j < m; ++j) {
143+
int a = nxt[i], b = nxt[j];
144+
ans += g[b].count(a);
145+
}
146+
}
147+
}
148+
return ans / 3;
149+
}
150+
};
151+
```
74152
153+
### **Go**
154+
155+
```go
156+
func numberOfPaths(n int, corridors [][]int) int {
157+
g := make([]map[int]bool, n+1)
158+
for i := range g {
159+
g[i] = make(map[int]bool)
160+
}
161+
for _, c := range corridors {
162+
a, b := c[0], c[1]
163+
g[a][b] = true
164+
g[b][a] = true
165+
}
166+
ans := 0
167+
for c := 1; c <= n; c++ {
168+
nxt := []int{}
169+
for v := range g[c] {
170+
nxt = append(nxt, v)
171+
}
172+
m := len(nxt)
173+
for i := 0; i < m; i++ {
174+
for j := i + 1; j < m; j++ {
175+
a, b := nxt[i], nxt[j]
176+
if g[b][a] {
177+
ans++
178+
}
179+
}
180+
}
181+
}
182+
return ans / 3
183+
}
75184
```
76185

77186
### **...**

solution/2000-2099/2077.Paths in Maze That Lead to Same Room/README_EN.md

+100-1
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,112 @@ There are no cycles of length 3.
5757
### **Python3**
5858

5959
```python
60-
60+
class Solution:
61+
def numberOfPaths(self, n: int, corridors: List[List[int]]) -> int:
62+
g = defaultdict(set)
63+
for a, b in corridors:
64+
g[a].add(b)
65+
g[b].add(a)
66+
ans = 0
67+
for i in range(1, n + 1):
68+
for j, k in combinations(g[i], 2):
69+
if j in g[k]:
70+
ans += 1
71+
return ans // 3
6172
```
6273

6374
### **Java**
6475

6576
```java
77+
class Solution {
78+
public int numberOfPaths(int n, int[][] corridors) {
79+
Set<Integer>[] g = new Set[n + 1];
80+
for (int i = 0; i <= n; ++i) {
81+
g[i] = new HashSet<>();
82+
}
83+
for (var c : corridors) {
84+
int a = c[0], b = c[1];
85+
g[a].add(b);
86+
g[b].add(a);
87+
}
88+
int ans = 0;
89+
for (int c = 1; c <= n; ++c) {
90+
var nxt = new ArrayList<>(g[c]);
91+
int m = nxt.size();
92+
for (int i = 0; i < m; ++i) {
93+
for (int j = i + 1; j < m; ++j) {
94+
int a = nxt.get(i), b = nxt.get(j);
95+
if (g[b].contains(a)) {
96+
++ans;
97+
}
98+
}
99+
}
100+
}
101+
return ans / 3;
102+
}
103+
}
104+
```
105+
106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
int numberOfPaths(int n, vector<vector<int>>& corridors) {
112+
vector<unordered_set<int>> g(n + 1);
113+
for (auto& c : corridors) {
114+
int a = c[0], b = c[1];
115+
g[a].insert(b);
116+
g[b].insert(a);
117+
}
118+
int ans = 0;
119+
for (int c = 1; c <= n; ++c) {
120+
vector<int> nxt;
121+
nxt.assign(g[c].begin(), g[c].end());
122+
int m = nxt.size();
123+
for (int i = 0; i < m; ++i) {
124+
for (int j = i + 1; j < m; ++j) {
125+
int a = nxt[i], b = nxt[j];
126+
ans += g[b].count(a);
127+
}
128+
}
129+
}
130+
return ans / 3;
131+
}
132+
};
133+
```
66134
135+
### **Go**
136+
137+
```go
138+
func numberOfPaths(n int, corridors [][]int) int {
139+
g := make([]map[int]bool, n+1)
140+
for i := range g {
141+
g[i] = make(map[int]bool)
142+
}
143+
for _, c := range corridors {
144+
a, b := c[0], c[1]
145+
g[a][b] = true
146+
g[b][a] = true
147+
}
148+
ans := 0
149+
for c := 1; c <= n; c++ {
150+
nxt := []int{}
151+
for v := range g[c] {
152+
nxt = append(nxt, v)
153+
}
154+
m := len(nxt)
155+
for i := 0; i < m; i++ {
156+
for j := i + 1; j < m; j++ {
157+
a, b := nxt[i], nxt[j]
158+
if g[b][a] {
159+
ans++
160+
}
161+
}
162+
}
163+
}
164+
return ans / 3
165+
}
67166
```
68167

69168
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int numberOfPaths(int n, vector<vector<int>>& corridors) {
4+
vector<unordered_set<int>> g(n + 1);
5+
for (auto& c : corridors) {
6+
int a = c[0], b = c[1];
7+
g[a].insert(b);
8+
g[b].insert(a);
9+
}
10+
int ans = 0;
11+
for (int c = 1; c <= n; ++c) {
12+
vector<int> nxt;
13+
nxt.assign(g[c].begin(), g[c].end());
14+
int m = nxt.size();
15+
for (int i = 0; i < m; ++i) {
16+
for (int j = i + 1; j < m; ++j) {
17+
int a = nxt[i], b = nxt[j];
18+
ans += g[b].count(a);
19+
}
20+
}
21+
}
22+
return ans / 3;
23+
}
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
func numberOfPaths(n int, corridors [][]int) int {
2+
g := make([]map[int]bool, n+1)
3+
for i := range g {
4+
g[i] = make(map[int]bool)
5+
}
6+
for _, c := range corridors {
7+
a, b := c[0], c[1]
8+
g[a][b] = true
9+
g[b][a] = true
10+
}
11+
ans := 0
12+
for c := 1; c <= n; c++ {
13+
nxt := []int{}
14+
for v := range g[c] {
15+
nxt = append(nxt, v)
16+
}
17+
m := len(nxt)
18+
for i := 0; i < m; i++ {
19+
for j := i + 1; j < m; j++ {
20+
a, b := nxt[i], nxt[j]
21+
if g[b][a] {
22+
ans++
23+
}
24+
}
25+
}
26+
}
27+
return ans / 3
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int numberOfPaths(int n, int[][] corridors) {
3+
Set<Integer>[] g = new Set[n + 1];
4+
for (int i = 0; i <= n; ++i) {
5+
g[i] = new HashSet<>();
6+
}
7+
for (var c : corridors) {
8+
int a = c[0], b = c[1];
9+
g[a].add(b);
10+
g[b].add(a);
11+
}
12+
int ans = 0;
13+
for (int c = 1; c <= n; ++c) {
14+
var nxt = new ArrayList<>(g[c]);
15+
int m = nxt.size();
16+
for (int i = 0; i < m; ++i) {
17+
for (int j = i + 1; j < m; ++j) {
18+
int a = nxt.get(i), b = nxt.get(j);
19+
if (g[b].contains(a)) {
20+
++ans;
21+
}
22+
}
23+
}
24+
}
25+
return ans / 3;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def numberOfPaths(self, n: int, corridors: List[List[int]]) -> int:
3+
g = defaultdict(set)
4+
for a, b in corridors:
5+
g[a].add(b)
6+
g[b].add(a)
7+
ans = 0
8+
for i in range(1, n + 1):
9+
for j, k in combinations(g[i], 2):
10+
if j in g[k]:
11+
ans += 1
12+
return ans // 3

0 commit comments

Comments
 (0)