Advertisement
ivandrofly

Medium Google Coding Interview With Ben Awad

Mar 1st, 2024
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. using System.Numerics;
  2.  
  3. int[,] matrix = {
  4.     { 1, 0, 0, 0, 0, 0 },
  5.     { 0, 1, 0, 1, 1, 1 },
  6.     { 0, 0, 1, 0, 1, 0 },
  7.     { 1, 1, 0, 0, 1, 0 },
  8.     { 1, 0, 1, 1, 0, 0 },
  9.     { 1, 0, 0, 0, 0, 1 },
  10. };
  11.  
  12. void Print(int[,] matrix)
  13. {
  14.     for (int i = 0; i < matrix.GetLength(0); i++)
  15.     {
  16.         for (int j = 0; j < matrix.GetLength(1); j++)
  17.         {
  18.             Console.Write(matrix[i, j] + ",");
  19.         }
  20.         Console.WriteLine();
  21.     }
  22. }
  23.  
  24. // b
  25. var linkBorder = new bool[matrix.GetLength(0), matrix.GetLength(1)];
  26.  
  27. // todo: explore this
  28. // var m = new Matrix3x2();
  29.  
  30. static void RemoveIslands(int[,] matrix, bool[,] borderLink)
  31. {
  32.     // int[,] directions = {
  33.     //     { 0, -1 }, // left
  34.     //     { 0, 1 }, // right
  35.     //     { 1, 0 }, // down
  36.     //     { -1, 0 }, // up
  37.     // };
  38.  
  39.     (int, int)[] directions = {
  40.         (0, -1), // left
  41.         (0, 1), // right
  42.         (1, 0), // down
  43.         (-1, 0), // up
  44.     };
  45.  
  46.     var rowCount = matrix.GetLength(0);
  47.     var colCount = matrix.GetLength(1);
  48.     for (int r = 0; r < rowCount; r++)
  49.     {
  50.         for (int c = 0; c < colCount; c++)
  51.         {
  52.             if (matrix[r, c] == 1 && IsOneBorderAdjacent(r, c, rowCount, colCount) == borderLink[r, c] == false)
  53.             {
  54.                 borderLink[r, c] = true;
  55.                 CheckNeighbors(r, c);
  56.             }
  57.         }
  58.     }
  59.  
  60.     for (int i = 0; i < borderLink.GetLength(0); i++)
  61.     {
  62.         for (int j = 0; j < borderLink.GetLength(1); j++)
  63.         {
  64.             if (borderLink[i, j] == false)
  65.             {
  66.                 matrix[i, j] = 0;
  67.             }
  68.         }
  69.     }
  70.    
  71.     void CheckNeighbors(int r, int c)
  72.     {
  73.         foreach ((int row, int col) direction in directions)
  74.         {
  75.             int nextRow = r + direction.row;
  76.             int nextCol = c + direction.col;
  77.             // invalid direction
  78.             if (!IsWithinPerimeter(nextRow, nextCol, rowCount, colCount) || matrix[nextRow, nextCol] != 1)
  79.             {
  80.                 continue;
  81.             }
  82.             if (matrix[nextRow, nextCol] == 1 && borderLink[nextRow, nextCol] == false)
  83.             {
  84.                 borderLink[nextRow, nextCol] = true;
  85.                 CheckNeighbors(nextRow, nextCol);
  86.             }
  87.         }
  88.     }
  89. }
  90.  
  91. // for a fized
  92. static bool IsOneBorderAdjacent(int row, int col, int rowCount, int colCount)
  93. {
  94.     if (!IsWithinPerimeter(row, col, rowCount, colCount)) return false;
  95.     return (row == 0 || row + 1 == rowCount || col + 1 == colCount);
  96. }
  97.  
  98. // validates if the coords are within bound to not go outside of perimeter of the matrix
  99. static bool IsWithinPerimeter(int row, int col, int rowCount, int colCount)
  100. {
  101.     return row >= 0 && row < rowCount && col >= 0 && col < colCount;
  102. }
  103.  
  104. static void Analyzes(int[,] matrix)
  105. {
  106.     foreach (int value in new[,] { { 1, 2 }, { 3, 4 } })
  107.     {
  108.         // output: 1,2,3,4
  109.         Console.WriteLine(value);
  110.     }
  111.  
  112.     Console.WriteLine(matrix.Length);
  113.     Console.WriteLine(matrix.GetLength(0));
  114.     Console.WriteLine(matrix.GetLength(1));
  115. }
  116.  
  117. Print(matrix);
  118. // Analyzes(matrix);
  119. RemoveIslands(matrix, linkBorder);
  120. Console.WriteLine("=========================");
  121. Print(matrix);
  122.  
  123. // Inspired from: https://www.youtube.com/watch?v=4tYoVx0QoN0&t=2518s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement