Skip to content

Commit 96d77e7

Browse files
committed
💀 [1791] Find Center of Star Graph - it is not even a graph problem ...
1 parent 42a7980 commit 96d77e7

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

1791/my_solution.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class Node {
2+
constructor(val) {
3+
this.val = val;
4+
this.neighbors = [];
5+
}
6+
}
7+
8+
const buildGraph = (adjList) => {
9+
const nodes = {};
10+
11+
// Create all the nodes
12+
for (let i = 0; i < adjList.length; i++) {
13+
const val = i + 1;
14+
nodes[val] = new Node(val);
15+
}
16+
17+
// Connect the nodes
18+
for (let i = 0; i < adjList.length; i++) {
19+
const node = nodes[i + 1]; // Get the corresponding Node object
20+
21+
for (let j = 0; j < adjList[i].length; j++) {
22+
const neighborVal = adjList[i][j];
23+
const neighborNode = nodes[neighborVal]; // Get the neighbor Node object
24+
node.neighbors.push(neighborNode);
25+
}
26+
}
27+
28+
// Return the reference to the first node (val = 1)
29+
return nodes[1];
30+
}
31+
32+
/**
33+
* @param {number[][]} edges
34+
* @return {number}
35+
*/
36+
var findCenter = function (edges) {
37+
let visited = new Map();
38+
39+
for (let i = 0; i < edges.length; i++) {
40+
for (let uv = 0; uv < edges[i].length; uv++) {
41+
if (visited.has(edges[i][uv])) {
42+
return edges[i][uv];
43+
}
44+
45+
visited.set(edges[i][uv], true)
46+
console.log(visited)
47+
}
48+
}
49+
};
50+
51+
// let x = buildGraph([[1, 2], [2, 3], [4, 2]])
52+
// let x = findCenter([[1, 2], [2, 3], [4, 2]])
53+
// let x = findCenter([[1, 3], [2, 3]])
54+
let x = findCenter([[1, 2], [5, 1], [1, 3], [1, 4]])
55+
console.log("x")
56+
console.log(x)
57+
// cloneGraph(node);

1791/solution.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number[][]} edges
3+
* @return {number}
4+
*/
5+
var findCenter = function (edges) {
6+
let visited = new Map();
7+
8+
for (let i = 0; i < edges.length; i++) {
9+
for (let uv = 0; uv < edges[i].length; uv++) {
10+
if (visited.has(edges[i][uv])) {
11+
return edges[i][uv];
12+
}
13+
14+
visited.set(edges[i][uv], true)
15+
}
16+
}
17+
};

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
- [424. Longest Repeating Character Replacement](./424/)
5656
- [647. Palindromic Substrings](./647/)
5757
- [771. Jewels and Stones](./771/)
58+
- [1791. Find Center of Star Graph](./1791/)
5859

5960
---
6061

@@ -88,5 +89,5 @@ Batch create in bash
8889
TODO: Add to TOC!
8990
-->
9091
```ssh
91-
chapter=133 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
92+
chapter=1791 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
9293
```

0 commit comments

Comments
 (0)