Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Amazon
  5. {
  6. class Program
  7. {
  8. //23280666352337
  9. static void Main(string[] args)
  10. {
  11.  
  12.  
  13. }
  14.  
  15. public int numberAmazonGoStores(int rows, int column, int[,] grid)
  16. {
  17.  
  18. // store the already visited rows and column
  19. var visited = new int[rows, column];
  20.  
  21. int numberOfBuildings = 0;
  22.  
  23. // iterate through all rows and columns to get number of buildings that Amazon can open stores in
  24. for (int currentRow = 0; currentRow < rows; currentRow++)
  25. {
  26.  
  27. for (int currentCol = 0; currentCol < column; currentCol++)
  28. {
  29.  
  30. numberOfBuildings += getNumberOfBuildings(grid, currentRow, currentCol, visited);
  31. }
  32. }
  33.  
  34. return numberOfBuildings;
  35.  
  36. }
  37.  
  38.  
  39. int getNumberOfBuildings(int[,] grid, int row, int col, int[,] visited)
  40. {
  41.  
  42.  
  43. if ((!isValid(grid, row, col)) || grid[row, col] == 0 || visited[row, col] == 1)
  44. {
  45. return 0;
  46. }
  47.  
  48. visited[row, col] = 1;
  49. // recursively go through the grid horizontally and vertically to find connected 1's
  50. getNumberOfBuildings(grid, row, col + 1, visited);
  51. getNumberOfBuildings(grid, row, col - 1, visited);
  52. getNumberOfBuildings(grid, row + 1, col, visited);
  53. getNumberOfBuildings(grid, row - 1, col, visited);
  54.  
  55. return 1;
  56. }
  57.  
  58. // checks if the requested positon is within bounds of the array
  59. bool isValid(int[,] grid, int row, int col)
  60. {
  61.  
  62. int rows = grid.GetLength(0);
  63. int cols = grid.GetLength(1);
  64.  
  65. return row >= 0 && row < rows && col >= 0 && col < cols;
  66. }
  67.  
  68. }
  69.  
  70. public class Solution2
  71. {
  72. // METHOD SIGNATURE BEGINS, THIS METHOD IS REQUIRED
  73. public int minimumHours(int rows, int columns, int[,] grid)
  74. {
  75. return CalculateHours(rows, columns, grid);
  76. // WRITE YOUR CODE HERE
  77. }
  78.  
  79.  
  80. private int CalculateHours(int rows, int columns, int[,] grid)
  81. {
  82. Queue<int[]> queue = new Queue<int[]>();
  83.  
  84. // total number of servers
  85. int target = rows * columns;
  86.  
  87. int count = 0, res = 0;
  88.  
  89. // iterate through all the rows and columns
  90. for (int i = 0; i < rows; i++)
  91. {
  92. for (int j = 0; j < columns; j++)
  93. {
  94. // if the current server has already received the file, add it to the queue
  95. if (grid[i, j] == 1)
  96. {
  97. queue.Enqueue(new int[] { i, j });
  98. count++;
  99. }
  100. }
  101. }
  102.  
  103. // directions: vertical: down and up, horizontal: left and right
  104. int[][] direction = GetDirections();
  105.  
  106. //Check for queue not empty
  107. while (queue.Count != 0)
  108. {
  109. int size = queue.Count;
  110.  
  111. if (count == target)
  112. return res;
  113.  
  114. for (int i = 0; i < size; i++)
  115. {
  116. int[] cur = queue.Dequeue();
  117.  
  118. // check all four directions: left, right, up, down
  119. // send files to ajdacent servers and set the server status to 1
  120. foreach (var dir in direction)
  121. {
  122. int ni = cur[0] + dir[0];
  123. int nj = cur[1] + dir[1];
  124. // check if the current direction is valid and within bounds
  125. if (ni >= 0 && ni < rows && nj >= 0 && nj < columns && grid[ni, nj] == 0)
  126. {
  127. count++;
  128. queue.Enqueue(new int[] { ni, nj });
  129. grid[ni, nj] = 1;
  130. }
  131. }
  132. }
  133.  
  134. res++;
  135. }
  136.  
  137. return -1;
  138. }
  139.  
  140. // directions: vertical: down and up, horizontal: left and right
  141. int[][] GetDirections()
  142. {
  143. int[][] direction = new int[4][];
  144.  
  145. direction[0] = new int[] { 0, 1 };
  146. direction[1] = new int[] { 0, -1 };
  147. direction[2] = new int[] { 1, 0 };
  148. direction[3] = new int[] { -1, 0 };
  149.  
  150. return direction;
  151. }
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement