Skip to content

Commit e9cde72

Browse files
committed
+ problem 2493
1 parent 8002848 commit e9cde72

File tree

5 files changed

+239
-0
lines changed

5 files changed

+239
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# 2493. Divide Nodes Into the Maximum Number of Groups
2+
You are given a positive integer `n` representing the number of nodes in an **undirected** graph. The nodes are labeled from `1` to `n`.
3+
4+
You are also given a 2D integer array `edges`, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is a **bidirectional** edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>. Notice that the given graph may be disconnected.
5+
6+
Divide the nodes of the graph into `m` groups (**1-indexed**) such that:
7+
* Each node in the graph belongs to exactly one group.
8+
* For every pair of nodes in the graph that are connected by an edge <code>[a<sub>i</sub>, b<sub>i</sub>]</code>, if <code>a<sub>i</sub></code> belongs to the group with index `x`, and <code>b<sub>i</sub></code> belongs to the group with index `y`, then `|y - x| = 1`.
9+
10+
Return *the maximum number of groups (i.e., maximum* `m`*) into which you can divide the nodes*. Return `-1` *if it is impossible to group the nodes with the given conditions*.
11+
12+
#### Example 1:
13+
![](https://assets.leetcode.com/uploads/2022/10/13/example1.png)
14+
<pre>
15+
<strong>Input:</strong> n = 6, edges = [[1,2],[1,4],[1,5],[2,6],[2,3],[4,6]]
16+
<strong>Output:</strong> 4
17+
<strong>Explanation:</strong> As shown in the image we:
18+
- Add node 5 to the first group.
19+
- Add node 1 to the second group.
20+
- Add nodes 2 and 4 to the third group.
21+
- Add nodes 3 and 6 to the fourth group.
22+
We can see that every edge is satisfied.
23+
It can be shown that that if we create a fifth group and move any node from the third or fourth group to it, at least on of the edges will not be satisfied.
24+
</pre>
25+
26+
#### Example 2:
27+
<pre>
28+
<strong>Input:</strong> n = 3, edges = [[1,2],[2,3],[3,1]]
29+
<strong>Output:</strong> -1
30+
<strong>Explanation:</strong> If we add node 1 to the first group, node 2 to the second group, and node 3 to the third group to satisfy the first two edges, we can see that the third edge will not be satisfied.
31+
It can be shown that no grouping is possible.
32+
</pre>
33+
34+
#### Constraints:
35+
* `1 <= n <= 500`
36+
* <code>1 <= edges.length <= 10<sup>4</sup></code>
37+
* `edges[i].length == 2`
38+
* <code>1 <= a<sub>i</sub>, b<sub>i</sub> <= n</code>
39+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
40+
* There is at most one edge between any pair of vertices.
41+
42+
## Solutions (Python)
43+
44+
### 1. Solution
45+
```Python
46+
class Solution:
47+
def magnificentSets(self, n: int, edges: List[List[int]]) -> int:
48+
parent = list(range(n + 1))
49+
neighbors = [[] for _ in range(n + 1)]
50+
unionfinds = {}
51+
ret = 0
52+
53+
for a, b in edges:
54+
while parent[a] != parent[parent[a]]:
55+
parent[a] = parent[parent[a]]
56+
while parent[b] != parent[parent[b]]:
57+
parent[b] = parent[parent[b]]
58+
59+
parent[parent[a]] = parent[b]
60+
neighbors[a].append(b)
61+
neighbors[b].append(a)
62+
63+
for node in range(1, n + 1):
64+
while parent[node] != parent[parent[node]]:
65+
parent[node] = parent[parent[node]]
66+
67+
if parent[node] not in unionfinds:
68+
unionfinds[parent[node]] = []
69+
unionfinds[parent[node]].append(node)
70+
71+
for unionfind in unionfinds.values():
72+
m = 1
73+
74+
for node in unionfind:
75+
used = {node: 1}
76+
queue = deque([node])
77+
78+
while queue:
79+
node = queue.popleft()
80+
i = used[node]
81+
82+
for neighbor in neighbors[node]:
83+
if neighbor not in used:
84+
used[neighbor] = i + 1
85+
queue.append(neighbor)
86+
m = max(m, i + 1)
87+
elif abs(used[neighbor] - i) != 1:
88+
return -1
89+
90+
ret += m
91+
92+
return ret
93+
```
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# 2493. 将节点分成尽可能多的组
2+
给你一个正整数 `n` ,表示一个 **无向** 图中的节点数目,节点编号从 `1``n`
3+
4+
同时给你一个二维整数数组 `edges` ,其中 <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 表示节点 <code>a<sub>i</sub></code> 和 <code>b<sub>i</sub></code> 之间有一条 **双向** 边。注意给定的图可能是不连通的。
5+
6+
请你将图划分为 `m` 个组(编号从 **1** 开始),满足以下要求:
7+
* 图中每个节点都只属于一个组。
8+
* 图中每条边连接的两个点 <code>[a<sub>i</sub>, b<sub>i</sub>]</code> ,如果 <code>a<sub>i</sub></code> 属于编号为 `x` 的组,<code>b<sub>i</sub></code> 属于编号为 `y` 的组,那么 `|y - x| = 1`
9+
10+
请你返回最多可以将节点分为多少个组(也就是最大的 `m` )。如果没办法在给定条件下分组,请你返回 `-1`
11+
12+
#### 示例 1:
13+
![](https://assets.leetcode.com/uploads/2022/10/13/example1.png)
14+
<pre>
15+
<strong>输入:</strong> n = 6, edges = [[1,2],[1,4],[1,5],[2,6],[2,3],[4,6]]
16+
<strong>输出:</strong> 4
17+
<strong>解释:</strong> 如上图所示,
18+
- 节点 5 在第一个组。
19+
- 节点 1 在第二个组。
20+
- 节点 2 和节点 4 在第三个组。
21+
- 节点 3 和节点 6 在第四个组。
22+
所有边都满足题目要求。
23+
如果我们创建第五个组,将第三个组或者第四个组中任何一个节点放到第五个组,至少有一条边连接的两个节点所属的组编号不符合题目要求。
24+
</pre>
25+
26+
#### 示例 2:
27+
<pre>
28+
<strong>输入:</strong> n = 3, edges = [[1,2],[2,3],[3,1]]
29+
<strong>输出:</strong> -1
30+
<strong>解释:</strong> 如果我们将节点 1 放入第一个组,节点 2 放入第二个组,节点 3 放入第三个组,前两条边满足题目要求,但第三条边不满足题目要求。
31+
没有任何符合题目要求的分组方式。
32+
</pre>
33+
34+
#### 提示:
35+
* `1 <= n <= 500`
36+
* <code>1 <= edges.length <= 10<sup>4</sup></code>
37+
* `edges[i].length == 2`
38+
* <code>1 <= a<sub>i</sub>, b<sub>i</sub> <= n</code>
39+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
40+
* 两个点之间至多只有一条边。
41+
42+
## 题解 (Python)
43+
44+
### 1. 题解
45+
```Python
46+
class Solution:
47+
def magnificentSets(self, n: int, edges: List[List[int]]) -> int:
48+
parent = list(range(n + 1))
49+
neighbors = [[] for _ in range(n + 1)]
50+
unionfinds = {}
51+
ret = 0
52+
53+
for a, b in edges:
54+
while parent[a] != parent[parent[a]]:
55+
parent[a] = parent[parent[a]]
56+
while parent[b] != parent[parent[b]]:
57+
parent[b] = parent[parent[b]]
58+
59+
parent[parent[a]] = parent[b]
60+
neighbors[a].append(b)
61+
neighbors[b].append(a)
62+
63+
for node in range(1, n + 1):
64+
while parent[node] != parent[parent[node]]:
65+
parent[node] = parent[parent[node]]
66+
67+
if parent[node] not in unionfinds:
68+
unionfinds[parent[node]] = []
69+
unionfinds[parent[node]].append(node)
70+
71+
for unionfind in unionfinds.values():
72+
m = 1
73+
74+
for node in unionfind:
75+
used = {node: 1}
76+
queue = deque([node])
77+
78+
while queue:
79+
node = queue.popleft()
80+
i = used[node]
81+
82+
for neighbor in neighbors[node]:
83+
if neighbor not in used:
84+
used[neighbor] = i + 1
85+
queue.append(neighbor)
86+
m = max(m, i + 1)
87+
elif abs(used[neighbor] - i) != 1:
88+
return -1
89+
90+
ret += m
91+
92+
return ret
93+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution:
2+
def magnificentSets(self, n: int, edges: List[List[int]]) -> int:
3+
parent = list(range(n + 1))
4+
neighbors = [[] for _ in range(n + 1)]
5+
unionfinds = {}
6+
ret = 0
7+
8+
for a, b in edges:
9+
while parent[a] != parent[parent[a]]:
10+
parent[a] = parent[parent[a]]
11+
while parent[b] != parent[parent[b]]:
12+
parent[b] = parent[parent[b]]
13+
14+
parent[parent[a]] = parent[b]
15+
neighbors[a].append(b)
16+
neighbors[b].append(a)
17+
18+
for node in range(1, n + 1):
19+
while parent[node] != parent[parent[node]]:
20+
parent[node] = parent[parent[node]]
21+
22+
if parent[node] not in unionfinds:
23+
unionfinds[parent[node]] = []
24+
unionfinds[parent[node]].append(node)
25+
26+
for unionfind in unionfinds.values():
27+
m = 1
28+
29+
for node in unionfind:
30+
used = {node: 1}
31+
queue = deque([node])
32+
33+
while queue:
34+
node = queue.popleft()
35+
i = used[node]
36+
37+
for neighbor in neighbors[node]:
38+
if neighbor not in used:
39+
used[neighbor] = i + 1
40+
queue.append(neighbor)
41+
m = max(m, i + 1)
42+
elif abs(used[neighbor] - i) != 1:
43+
return -1
44+
45+
ret += m
46+
47+
return ret

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,7 @@
17701770
[2488][2488l]|[Count Subarrays With Median K][2488] |![rs]
17711771
[2490][2490l]|[Circular Sentence][2490] |![py]
17721772
[2491][2491l]|[Divide Players Into Teams of Equal Skill][2491] |![rs]
1773+
[2493][2493l]|[Divide Nodes Into the Maximum Number of Groups][2493] |![py]
17731774
[2496][2496l]|[Maximum Value of a String in an Array][2496] |![rs]
17741775
[2497][2497l]|[Maximum Star Sum of a Graph][2497] |![rs]
17751776
[2498][2498l]|[Frog Jump II][2498] |![rs]
@@ -3641,6 +3642,7 @@
36413642
[2488]:Problemset/2488-Count%20Subarrays%20With%20Median%20K/README.md#2488-count-subarrays-with-median-k
36423643
[2490]:Problemset/2490-Circular%20Sentence/README.md#2490-circular-sentence
36433644
[2491]:Problemset/2491-Divide%20Players%20Into%20Teams%20of%20Equal%20Skill/README.md#2491-divide-players-into-teams-of-equal-skill
3645+
[2493]:Problemset/2493-Divide%20Nodes%20Into%20the%20Maximum%20Number%20of%20Groups/README.md#2493-divide-nodes-into-the-maximum-number-of-groups
36443646
[2496]:Problemset/2496-Maximum%20Value%20of%20a%20String%20in%20an%20Array/README.md#2496-maximum-value-of-a-string-in-an-array
36453647
[2497]:Problemset/2497-Maximum%20Star%20Sum%20of%20a%20Graph/README.md#2497-maximum-star-sum-of-a-graph
36463648
[2498]:Problemset/2498-Frog%20Jump%20II/README.md#2498-frog-jump-ii
@@ -5506,6 +5508,7 @@
55065508
[2488l]:https://leetcode.com/problems/count-subarrays-with-median-k/
55075509
[2490l]:https://leetcode.com/problems/circular-sentence/
55085510
[2491l]:https://leetcode.com/problems/divide-players-into-teams-of-equal-skill/
5511+
[2493l]:https://leetcode.com/problems/divide-nodes-into-the-maximum-number-of-groups/
55095512
[2496l]:https://leetcode.com/problems/maximum-value-of-a-string-in-an-array/
55105513
[2497l]:https://leetcode.com/problems/maximum-star-sum-of-a-graph/
55115514
[2498l]:https://leetcode.com/problems/frog-jump-ii/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,7 @@
17701770
[2488][2488l]|[统计中位数为 K 的子数组][2488] |![rs]
17711771
[2490][2490l]|[回环句][2490] |![py]
17721772
[2491][2491l]|[划分技能点相等的团队][2491] |![rs]
1773+
[2493][2493l]|[将节点分成尽可能多的组][2493] |![py]
17731774
[2496][2496l]|[数组中字符串的最大值][2496] |![rs]
17741775
[2497][2497l]|[图中最大星和][2497] |![rs]
17751776
[2498][2498l]|[青蛙过河 II][2498] |![rs]
@@ -3641,6 +3642,7 @@
36413642
[2488]:Problemset/2488-Count%20Subarrays%20With%20Median%20K/README_CN.md#2488-统计中位数为-k-的子数组
36423643
[2490]:Problemset/2490-Circular%20Sentence/README_CN.md#2490-回环句
36433644
[2491]:Problemset/2491-Divide%20Players%20Into%20Teams%20of%20Equal%20Skill/README_CN.md#2491-划分技能点相等的团队
3645+
[2493]:Problemset/2493-Divide%20Nodes%20Into%20the%20Maximum%20Number%20of%20Groups/README_CN.md#2493-将节点分成尽可能多的组
36443646
[2496]:Problemset/2496-Maximum%20Value%20of%20a%20String%20in%20an%20Array/README_CN.md#2496-数组中字符串的最大值
36453647
[2497]:Problemset/2497-Maximum%20Star%20Sum%20of%20a%20Graph/README_CN.md#2497-图中最大星和
36463648
[2498]:Problemset/2498-Frog%20Jump%20II/README_CN.md#2498-frog-jump-ii
@@ -5506,6 +5508,7 @@
55065508
[2488l]:https://leetcode.cn/problems/count-subarrays-with-median-k/
55075509
[2490l]:https://leetcode.cn/problems/circular-sentence/
55085510
[2491l]:https://leetcode.cn/problems/divide-players-into-teams-of-equal-skill/
5511+
[2493l]:https://leetcode.cn/problems/divide-nodes-into-the-maximum-number-of-groups/
55095512
[2496l]:https://leetcode.cn/problems/maximum-value-of-a-string-in-an-array/
55105513
[2497l]:https://leetcode.cn/problems/maximum-star-sum-of-a-graph/
55115514
[2498l]:https://leetcode.com/problems/frog-jump-ii/

0 commit comments

Comments
 (0)