Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Last active November 6, 2025 17:41
Show Gist options
  • Select an option

  • Save tatsuyax25/ebe76bfd23b3b24ef75be5f27e1097bf to your computer and use it in GitHub Desktop.

Select an option

Save tatsuyax25/ebe76bfd23b3b24ef75be5f27e1097bf to your computer and use it in GitHub Desktop.
You are given an integer c representing c power stations, each with a unique identifier id from 1 to c (1‑based indexing). These stations are interconnected via n bidirectional cables, represented by a 2D array connections, where each element connec
/**
* Simulates maintenance and shutdown queries on a network of power stations.
*
* @param {number} c - Number of power stations (1-indexed).
* @param {number[][]} connections - Bidirectional cables between stations.
* @param {number[][]} queries - List of queries: [1, x] for maintenance, [2, x] to shut down.
* @return {number[]} - Results of maintenance queries.
*/
var processQueries = function(c, connections, queries) {
// ----- Union-Find (Disjoint Set Union) Setup -----
const parent = Array(c + 1).fill(0).map((_, i) => i); // parent[i] = i
const rank = Array(c + 1).fill(0); // Used for union by rank
// Find with path compression
const find = (x) => {
while (parent[x] !== x) {
parent[x] = parent[parent[x]]; // Path compression
x = parent[x];
}
return x;
};
// Union by rank
const union = (a, b) => {
const ra = find(a), rb = find(b);
if (ra === rb) return;
if (rank[ra] < rank[rb]) {
parent[ra] = rb;
} else if (rank[rb] < rank[ra]) {
parent[rb] = ra;
} else {
parent[rb] = ra;
rank[ra]++;
}
};
// Merge all connected stations into components
for (const [u, v] of connections) {
union(u, v);
}
// ----- Build Component Memberships -----
const compMembers = new Map(); // root -> sorted array of member stations
for (let i = 1; i <= c; i++) {
const root = find(i);
if (!compMembers.has(root)) compMembers.set(root, []);
compMembers.get(root).push(i);
}
// Sort each component's members for efficient min lookup
for (const members of compMembers.values()) {
members.sort((a, b) => a - b);
}
// ----- Track Online Status and Component Pointers -----
const online = Array(c + 1).fill(true); // All stations start online
const compPtr = new Map(); // root -> index of smallest online station in component
for (const root of compMembers.keys()) {
compPtr.set(root, 0);
}
// Helper: Find smallest online station in x's component
const smallestOnlineInComp = (x) => {
const root = find(x);
const members = compMembers.get(root) || [];
let i = compPtr.get(root) ?? 0;
// Advance pointer to next online station
while (i < members.length && !online[members[i]]) {
i++;
}
compPtr.set(root, i); // Update pointer
return i < members.length ? members[i] : -1;
};
// ----- Process Queries -----
const result = [];
for (const [type, x] of queries) {
if (type === 1) {
// Maintenance check
result.push(online[x] ? x : smallestOnlineInComp(x));
} else {
// Shutdown
online[x] = false;
}
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment