I'm starting my own question because I can't find anything directly related to c# and the solution I am using is different approach. The question is described as follows
In a given grid, each cell can have one of three values:
the value 0 representing an empty cell;
the value 1 representing a fresh orange;
the value 2 representing a rotten orange.
Every minute, any fresh orange that is adjacent (4-directionally) to a rotten orange becomes rotten.
Return the minimum number of minutes that must elapse until no cell has a fresh orange.
If this is impossible, return -1 instead.
Example 1:
Input: [[2,1,1],[1,1,0],[0,1,1]] Output: 4
Example 2:
Input: [[2,1,1],[0,1,1],[1,0,1]] Output: -1 Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
Example 3:
Input: [[0,2]] Output: 0 Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.
Note:
1 <= grid.length <= 10
1 <= grid[0].length <= 10
grid[i][j] is only 0, 1, or 2.
I currently have the following as my code:
public class Solution {
public int OrangesRotting(int[][] grid) {
var rows = grid.Length;
var cols = grid[0].Length;
HashSet<string> fresh = new HashSet<string>();
HashSet<string> rotten = new HashSet<string>();
for(var i = 0; i < rows; i++){
for(var j = 0; j < cols; j++){
var current = grid[i][j];
if(current == 1){
fresh.Add(""+i+j);
} else if (current == 2){
rotten.Add(""+i+j);
}
}
}
int minutes = 0;
int[,] directions = {{0,1},{1,0},{-1,0},{0,-1}};
while(fresh.Count > 0){
HashSet<string> infected = new HashSet<string>();
foreach(string s in rotten){
int i = s[0] - '0';
int j = s[1] - '0';
foreach(int[] direction in directions){
int nextI = i + direction[0];
int nextJ = j + direction[1];
if(fresh.Contains(""+nextI+nextJ)){
fresh.Remove(""+nextI+nextJ);
infected.Add(""+nextI+nextJ);
}
}
}
if(infected.Count == 0) return -1;
minutes++;
}
return minutes;
}
}
The issue I am having is I get the following compile error:
Line 29: Char 17: error CS0030: Cannot convert type 'int' to 'int[]' (in Solution.cs)