Skip to content

Commit 8dfb438

Browse files
committed
Add solution #2493
1 parent 579af71 commit 8dfb438

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@
481481
2469|[Convert the Temperature](./2469-convert-the-temperature.js)|Easy|
482482
2482|[Difference Between Ones and Zeros in Row and Column](./2482-difference-between-ones-and-zeros-in-row-and-column.js)|Medium|
483483
2490|[Circular Sentence](./2490-circular-sentence.js)|Easy|
484+
2493|[Divide Nodes Into the Maximum Number of Groups](./2493-divide-nodes-into-the-maximum-number-of-groups.js)|Hard|
484485
2529|[Maximum Count of Positive Integer and Negative Integer](./2529-maximum-count-of-positive-integer-and-negative-integer.js)|Easy|
485486
2535|[Difference Between Element Sum and Digit Sum of an Array](./2535-difference-between-element-sum-and-digit-sum-of-an-array.js)|Easy|
486487
2618|[Check if Object Instance of Class](./2618-check-if-object-instance-of-class.js)|Medium|
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* 2493. Divide Nodes Into the Maximum Number of Groups
3+
* https://leetcode.com/problems/divide-nodes-into-the-maximum-number-of-groups/
4+
* Difficulty: Hard
5+
*
6+
* You are given a positive integer n representing the number of nodes in an undirected graph.
7+
* The nodes are labeled from 1 to n.
8+
*
9+
* You are also given a 2D integer array edges, where edges[i] = [ai, bi] indicates that there
10+
* is a bidirectional edge between nodes ai and bi. Notice that the given graph may be disconnected.
11+
*
12+
* Divide the nodes of the graph into m groups (1-indexed) such that:
13+
* - Each node in the graph belongs to exactly one group.
14+
* - For every pair of nodes in the graph that are connected by an edge [ai, bi], if ai belongs to
15+
* the group with index x, and bi belongs to the group with index y, then |y - x| = 1.
16+
*
17+
* Return the maximum number of groups (i.e., maximum m) into which you can divide the nodes.
18+
* Return -1 if it is impossible to group the nodes with the given conditions.
19+
*/
20+
21+
/**
22+
* @param {number} n
23+
* @param {number[][]} edges
24+
* @return {number}
25+
*/
26+
var magnificentSets = function(n, edges) {
27+
const graph = new Array(n).fill().map(() => []);
28+
for (const [i, j] of edges) {
29+
graph[i - 1].push(j - 1);
30+
graph[j - 1].push(i - 1);
31+
}
32+
33+
const result = new Array(n).fill(0);
34+
for (let i = 0; i < n; i++) {
35+
const groups = new Array(n).fill(0);
36+
const queue = [i];
37+
let max = 1;
38+
let root = i;
39+
40+
groups[i] = 1;
41+
42+
while (queue.length) {
43+
const key = queue.shift();
44+
root = Math.min(root, key);
45+
46+
for (const node of graph[key]) {
47+
if (groups[node] === 0) {
48+
groups[node] = groups[key] + 1;
49+
max = Math.max(max, groups[node]);
50+
queue.push(node);
51+
} else if (Math.abs(groups[node] - groups[key]) !== 1) {
52+
return -1;
53+
}
54+
}
55+
}
56+
57+
result[root] = Math.max(result[root], max);
58+
}
59+
60+
return result.reduce((a, b) => a + b);
61+
};

0 commit comments

Comments
 (0)