Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 34f5f0c

Browse files
feat: add solutions to lc problem: No.3493 (#4290)
No.3493.Properties Graph
1 parent 4b39d60 commit 34f5f0c

File tree

7 files changed

+662
-8
lines changed

7 files changed

+662
-8
lines changed

‎solution/3400-3499/3493.Properties Graph/README.md‎

Lines changed: 225 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,32 +81,253 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3493.Pr
8181

8282
<!-- solution:start -->
8383

84-
### 方法一
84+
### 方法一:哈希表 + DFS
85+
86+
我们先将每个属性数组转换为一个哈希表,存储在哈希表数组 $\textit{ss}$ 中。定义一个图 $\textit{g},ドル其中 $\textit{g}[i]$ 存储了与属性数组 $\textit{properties}[i]$ 有边相连的属性数组的索引。
87+
88+
然后我们遍历所有的属性哈希表,对于每一对属性哈希表 $(i, j),ドル其中 $j < i,ドル我们检查这两个属性哈希表中的交集元素个数是否大于等于 $k,ドル如果是,则在图 $\textit{g}$ 中添加一条从 $i$ 到 $j$ 的边,同时在图 $\textit{g}$ 中添加一条从 $j$ 到 $i$ 的边。
89+
90+
最后,我们使用深度优先搜索计算图 $\textit{g}$ 的连通分量的数量。
91+
92+
时间复杂度 $O(n^2 \times m),ドル空间复杂度 $O(n \times m)$。其中 $n$ 是属性数组的长度,而 $m$ 是属性数组中的元素个数。
8593

8694
<!-- tabs:start -->
8795

8896
#### Python3
8997

9098
```python
91-
99+
class Solution:
100+
def numberOfComponents(self, properties: List[List[int]], k: int) -> int:
101+
def dfs(i: int) -> None:
102+
vis[i] = True
103+
for j in g[i]:
104+
if not vis[j]:
105+
dfs(j)
106+
107+
n = len(properties)
108+
ss = list(map(set, properties))
109+
g = [[] for _ in range(n)]
110+
for i, s1 in enumerate(ss):
111+
for j in range(i):
112+
s2 = ss[j]
113+
if len(s1 & s2) >= k:
114+
g[i].append(j)
115+
g[j].append(i)
116+
ans = 0
117+
vis = [False] * n
118+
for i in range(n):
119+
if not vis[i]:
120+
dfs(i)
121+
ans += 1
122+
return ans
92123
```
93124

94125
#### Java
95126

96127
```java
97-
128+
class Solution {
129+
private List<Integer>[] g;
130+
private boolean[] vis;
131+
132+
public int numberOfComponents(int[][] properties, int k) {
133+
int n = properties.length;
134+
g = new List[n];
135+
Set<Integer>[] ss = new Set[n];
136+
Arrays.setAll(g, i -> new ArrayList<>());
137+
Arrays.setAll(ss, i -> new HashSet<>());
138+
for (int i = 0; i < n; ++i) {
139+
for (int x : properties[i]) {
140+
ss[i].add(x);
141+
}
142+
}
143+
for (int i = 0; i < n; ++i) {
144+
for (int j = 0; j < i; ++j) {
145+
int cnt = 0;
146+
for (int x : ss[i]) {
147+
if (ss[j].contains(x)) {
148+
++cnt;
149+
}
150+
}
151+
if (cnt >= k) {
152+
g[i].add(j);
153+
g[j].add(i);
154+
}
155+
}
156+
}
157+
158+
int ans = 0;
159+
vis = new boolean[n];
160+
for (int i = 0; i < n; ++i) {
161+
if (!vis[i]) {
162+
dfs(i);
163+
++ans;
164+
}
165+
}
166+
return ans;
167+
}
168+
169+
private void dfs(int i) {
170+
vis[i] = true;
171+
for (int j : g[i]) {
172+
if (!vis[j]) {
173+
dfs(j);
174+
}
175+
}
176+
}
177+
}
98178
```
99179

100180
#### C++
101181

102182
```cpp
103-
183+
class Solution {
184+
public:
185+
int numberOfComponents(vector<vector<int>>& properties, int k) {
186+
int n = properties.size();
187+
unordered_set<int> ss[n];
188+
vector<int> g[n];
189+
for (int i = 0; i < n; ++i) {
190+
for (int x : properties[i]) {
191+
ss[i].insert(x);
192+
}
193+
}
194+
for (int i = 0; i < n; ++i) {
195+
auto& s1 = ss[i];
196+
for (int j = 0; j < i; ++j) {
197+
auto& s2 = ss[j];
198+
int cnt = 0;
199+
for (int x : s1) {
200+
if (s2.contains(x)) {
201+
++cnt;
202+
}
203+
}
204+
if (cnt >= k) {
205+
g[i].push_back(j);
206+
g[j].push_back(i);
207+
}
208+
}
209+
}
210+
int ans = 0;
211+
vector<bool> vis(n);
212+
auto dfs = [&](this auto&& dfs, int i) -> void {
213+
vis[i] = true;
214+
for (int j : g[i]) {
215+
if (!vis[j]) {
216+
dfs(j);
217+
}
218+
}
219+
};
220+
for (int i = 0; i < n; ++i) {
221+
if (!vis[i]) {
222+
dfs(i);
223+
++ans;
224+
}
225+
}
226+
return ans;
227+
}
228+
};
104229
```
105230
106231
#### Go
107232
108233
```go
234+
func numberOfComponents(properties [][]int, k int) (ans int) {
235+
n := len(properties)
236+
ss := make([]map[int]struct{}, n)
237+
g := make([][]int, n)
238+
239+
for i := 0; i < n; i++ {
240+
ss[i] = make(map[int]struct{})
241+
for _, x := range properties[i] {
242+
ss[i][x] = struct{}{}
243+
}
244+
}
245+
246+
for i := 0; i < n; i++ {
247+
for j := 0; j < i; j++ {
248+
cnt := 0
249+
for x := range ss[i] {
250+
if _, ok := ss[j][x]; ok {
251+
cnt++
252+
}
253+
}
254+
if cnt >= k {
255+
g[i] = append(g[i], j)
256+
g[j] = append(g[j], i)
257+
}
258+
}
259+
}
260+
261+
vis := make([]bool, n)
262+
var dfs func(int)
263+
dfs = func(i int) {
264+
vis[i] = true
265+
for _, j := range g[i] {
266+
if !vis[j] {
267+
dfs(j)
268+
}
269+
}
270+
}
271+
272+
for i := 0; i < n; i++ {
273+
if !vis[i] {
274+
dfs(i)
275+
ans++
276+
}
277+
}
278+
return
279+
}
280+
```
109281

282+
#### TypeScript
283+
284+
```ts
285+
function numberOfComponents(properties: number[][], k: number): number {
286+
const n = properties.length;
287+
const ss: Set<number>[] = Array.from({ length: n }, () => new Set());
288+
const g: number[][] = Array.from({ length: n }, () => []);
289+
290+
for (let i = 0; i < n; i++) {
291+
for (const x of properties[i]) {
292+
ss[i].add(x);
293+
}
294+
}
295+
296+
for (let i = 0; i < n; i++) {
297+
for (let j = 0; j < i; j++) {
298+
let cnt = 0;
299+
for (const x of ss[i]) {
300+
if (ss[j].has(x)) {
301+
cnt++;
302+
}
303+
}
304+
if (cnt >= k) {
305+
g[i].push(j);
306+
g[j].push(i);
307+
}
308+
}
309+
}
310+
311+
let ans = 0;
312+
const vis: boolean[] = Array(n).fill(false);
313+
314+
const dfs = (i: number) => {
315+
vis[i] = true;
316+
for (const j of g[i]) {
317+
if (!vis[j]) {
318+
dfs(j);
319+
}
320+
}
321+
};
322+
323+
for (let i = 0; i < n; i++) {
324+
if (!vis[i]) {
325+
dfs(i);
326+
ans++;
327+
}
328+
}
329+
return ans;
330+
}
110331
```
111332

112333
<!-- tabs:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /