Skip to content

Commit 2bd69fa

Browse files
.
1 parent cb9d212 commit 2bd69fa

File tree

2 files changed

+118
-2
lines changed

2 files changed

+118
-2
lines changed

Algorithms/Medium_Algorithms.Tests/UnitTest1.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,10 +743,42 @@ public static bool compare(List<string> arr1, string[] arr2)
743743
}
744744
}
745745
return true;
746-
}
746+
}
747747
#endregion
748748

749+
#region RiverSizesClass
750+
[Fact]
751+
public void RiverSizesClassTestCase1()
752+
{
753+
int[,] input = {
754+
{ 1, 0, 0, 1, 0 },
755+
{ 1, 0, 1, 0, 0 },
756+
{ 0, 0, 1, 0, 1 },
757+
{ 1, 0, 1, 0, 1 },
758+
{ 1, 0, 1, 1, 0 },
759+
};
760+
int[] expected = { 1, 2, 2, 2, 5 };
761+
List<int> output = RiverSizesClass.RiverSizes(input);
762+
output.Sort();
763+
Assert.True(compare(output, expected));
764+
}
749765

766+
public static bool compare(List<int> arr1, int[] arr2)
767+
{
768+
if (arr1.Count != arr2.Length)
769+
{
770+
return false;
771+
}
772+
for (int i = 0; i < arr1.Count; i++)
773+
{
774+
if (arr1[i] != arr2[i])
775+
{
776+
return false;
777+
}
778+
}
779+
return true;
780+
}
781+
#endregion
750782

751783

752784

Algorithms/Medium_Algorithms/MediumAlgorithmsClass.cs

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1525,9 +1525,93 @@ public Node AddChild(string name)
15251525
return this;
15261526
}
15271527
}
1528-
}
1528+
}
15291529
#endregion
15301530

1531+
#region RiverSizesClass
1532+
/// <summary>
1533+
///
1534+
/// </summary>
1535+
public class RiverSizesClass
1536+
{
1537+
public static List<int> RiverSizes(int[,] matrix)
1538+
{
1539+
List<int> sizes = new List<int>();
1540+
bool[,] visited = new bool[matrix.GetLength(0), matrix.GetLength(1)];
1541+
for (int i = 0; i < matrix.GetLength(0); i++)
1542+
{
1543+
for (int j = 0; j < matrix.GetLength(1); j++)
1544+
{
1545+
if (visited[i, j])
1546+
{
1547+
continue;
1548+
}
1549+
traverseNode(i, j, matrix, visited, sizes);
1550+
}
1551+
}
1552+
return sizes;
1553+
}
1554+
1555+
public static void traverseNode(int i, int j, int[,] matrix, bool[,] visited, List<int> sizes)
1556+
{
1557+
int currentRiverSize = 0;
1558+
List<int[]> nodesToExplore = new List<int[]>();
1559+
nodesToExplore.Add(new int[] { i, j });
1560+
while (nodesToExplore.Count != 0)
1561+
{
1562+
int[] currentNode = nodesToExplore[nodesToExplore.Count - 1];
1563+
nodesToExplore.RemoveAt(nodesToExplore.Count - 1);
1564+
i = currentNode[0];
1565+
j = currentNode[1];
1566+
if (visited[i, j])
1567+
{
1568+
continue;
1569+
}
1570+
visited[i, j] = true;
1571+
if (matrix[i, j] == 0)
1572+
{
1573+
continue;
1574+
}
1575+
currentRiverSize++;
1576+
List<int[]> unvisitedNeighbors =
1577+
getUnvisitedNeighbors(i, j, matrix, visited);
1578+
foreach (int[] neighbor in unvisitedNeighbors)
1579+
{
1580+
nodesToExplore.Add(neighbor);
1581+
}
1582+
}
1583+
if (currentRiverSize > 0)
1584+
{
1585+
sizes.Add(currentRiverSize);
1586+
}
1587+
}
1588+
1589+
public static List<int[]> getUnvisitedNeighbors(int i, int j, int[,] matrix, bool[,] visited)
1590+
{
1591+
List<int[]> unvisitedNeighbors = new List<int[]>();
1592+
if (i > 0 && !visited[i - 1, j])
1593+
{
1594+
unvisitedNeighbors.Add(new int[] {i - 1, j
1595+
});
1596+
}
1597+
1598+
if (i < matrix.GetLength(0) - 1 && !visited[i + 1, j])
1599+
{
1600+
unvisitedNeighbors.Add(new int[] { i + 1, j });
1601+
}
1602+
if (j > 0 && !visited[i, j - 1])
1603+
{
1604+
unvisitedNeighbors.Add(new int[] { i, j - 1 });
1605+
}
1606+
1607+
if (j < matrix.GetLength(1) - 1 && !visited[i, j + 1])
1608+
{
1609+
unvisitedNeighbors.Add(new int[] { i, j + 1 });
1610+
}
1611+
return unvisitedNeighbors;
1612+
}
1613+
}
1614+
#endregion
15311615

15321616

15331617

0 commit comments

Comments
 (0)