en C#, /** * @param nCogwheels an integer representing the number of cogwheels in the system (numbered from 0 to [[params.nCogwheels]]-1). * @param connections [[anArray]] representing all pairs of cogwheels connected together. * @return [[anArray]] of two integers, representing the number of cogwheels rotating clockwise and counterclockwise respectively, or [-1, -1] in case of a deadlock. */ public static List<int> CountRotations(int nCogwheels, List<List<int>> connections) the parameter nCogwheels defines the number of cogwheels in the system. THey are identified by numbers from 0 to nCogwheels-1. The connections is a list descripbing how they are organized. Each elemnt contains 2 cogwheel identifiers, to specify they are connected. Guaranted to have a connected gear system. at the beginning the cogwheel number 0 rotates clockwise. When 2 cogwheels are connected, they rotate in opposite directions. You have to return list of 2 integers: the number of cogwheels rotating clockwise, the number of cogwheels rotating counter-clockwise. The gear system may be in a deadlock. It happens when the connections force one of the cogwheel to ratoate in both of the 2 directions. In that case, return the list [-1,-1]
To solve this problem in C#, we can model the cogwheels as a graph, where each cogwheel is a node and each connection is an undirected edge. The main idea is:
-
Start from cogwheel 0 (which is rotating clockwise).
-
Use BFS or DFS to propagate alternating rotation directions (
clockwise
= 1,counter-clockwise
= -1) to connected cogwheels. -
If we ever encounter a conflict (i.e. a cogwheel already visited but with an inconsistent rotation), we return
[-1, -1]
for deadlock.
✅ Final C# Code
using System;
using System.Collections.Generic;
public class Solution
{
public static List<int> CountRotations(int nCogwheels, List<List<int>> connections)
{
// Build adjacency list
var graph = new Dictionary<int, List<int>>();
for (int i = 0; i < nCogwheels; i++)
graph[i] = new List<int>();
foreach (var pair in connections)
{
int a = pair[0];
int b = pair[1];
graph[a].Add(b);
graph[b].Add(a);
}
// -1: unvisited, 0: clockwise, 1: counter-clockwise
int[] rotation = new int[nCogwheels];
Array.Fill(rotation, -1);
Queue<int> queue = new Queue<int>();
queue.Enqueue(0);
rotation[0] = 0; // start with clockwise
while (queue.Count > 0)
{
int current = queue.Dequeue();
int currentDir = rotation[current];
foreach (int neighbor in graph[current])
{
if (rotation[neighbor] == -1)
{
rotation[neighbor] = 1 - currentDir; // alternate direction
queue.Enqueue(neighbor);
}
else if (rotation[neighbor] == currentDir)
{
// Conflict: same direction on connected gears → deadlock
return new List<int> { -1, -1 };
}
}
}
// Count rotations
int clockwise = 0, counterClockwise = 0;
foreach (int dir in rotation)
{
if (dir == 0) clockwise++;
else if (dir == 1) counterClockwise++;
}
return new List<int> { clockwise, counterClockwise };
}
}
🔍 Example
Input:
int nCogwheels = 4;
var connections = new List<List<int>> {
new List<int>{0, 1},
new List<int>{1, 2},
new List<int>{2, 3}
};
Output:
[2, 2] // cogwheels 0 and 2 rotate clockwise; 1 and 3 counter-clockwise
Deadlock Case:
var connections = new List<List<int>> {
new List<int>{0, 1},
new List<int>{1, 2},
new List<int>{2, 0}
};
→ This forms a cycle of 3 gears, which causes a conflict (odd-length cycle) → output will be [-1, -1]
========================================================
Depth-First Search (DFS) instead of BFS. It works in the same way: alternates directions between connected cogwheels, detects deadlocks, and counts the rotations.
✅ DFS-Based C# Implementation
using System;
using System.Collections.Generic;
public class Solution
{
public static List<int> CountRotations(int nCogwheels, List<List<int>> connections)
{
// Build adjacency list
var graph = new Dictionary<int, List<int>>();
for (int i = 0; i < nCogwheels; i++)
graph[i] = new List<int>();
foreach (var pair in connections)
{
int a = pair[0], b = pair[1];
graph[a].Add(b);
graph[b].Add(a);
}
// rotation: -1 = unvisited, 0 = clockwise, 1 = counter-clockwise
int[] rotation = new int[nCogwheels];
Array.Fill(rotation, -1);
bool hasDeadlock = false;
void DFS(int current, int dir)
{
rotation[current] = dir;
foreach (int neighbor in graph[current])
{
if (rotation[neighbor] == -1)
{
DFS(neighbor, 1 - dir);
}
else if (rotation[neighbor] == dir)
{
hasDeadlock = true;
return;
}
}
}
DFS(0, 0); // start DFS from gear 0, clockwise
if (hasDeadlock)
return new List<int> { -1, -1 };
int clockwise = 0, counterClockwise = 0;
foreach (int dir in rotation)
{
if (dir == 0) clockwise++;
else if (dir == 1) counterClockwise++;
}
return new List<int> { clockwise, counterClockwise };
}
}
🔍 Notes:
-
DFS explores the graph recursively.
-
We assign alternating directions as we move to neighbors.
-
If a conflict is found (same direction on connected nodes), we mark a deadlock.
-
Finally, count the rotations.
Example Use:
var result = Solution.CountRotations(4, new List<List<int>> {
new List<int>{0, 1},
new List<int>{1, 2},
new List<int>{2, 3}
});
// Output: [2, 2]
Aucun commentaire:
Enregistrer un commentaire