Created
April 9, 2021 01:39
-
-
Save NightOwlCoder/b413dbedf9502294e45a29803b016915 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
namespace interviews | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
ConnectedColor.run(new[] | |
{ | |
"4 5", | |
"RBYBR", | |
"BYYRY", | |
"RBYBB", | |
"RBYRY", | |
}, new[] | |
{ | |
"R=2", | |
"B=2", | |
"Y=5", | |
}); | |
ConnectedColor.run(new[] | |
{ | |
"3 4", | |
"GGBR", | |
"GBRB", | |
"RBBB", | |
}, new[] | |
{ | |
"G=3", | |
"B=5", | |
"R=1", | |
}); | |
} | |
} | |
public class ConnectedColor | |
{ | |
/** | |
* Given a color grid, return a map of all colors inside the grid with | |
* their maximum number of connected occurrences. | |
* | |
* A color of a cell is connected if another neighboring cell contains | |
* the same color. Connected only on top/bottom, right/left, no diagonals. | |
* | |
* Runtime is O(n*m) where n and m are the height/width of the grid. | |
*/ | |
public static void run(string[] gameSetup, string[] answers) | |
{ | |
var currLine = 0; | |
var answer = ""; | |
var answerP = 0; | |
var tokens_n = gameSetup[currLine++].Split(' '); | |
var rows = Convert.ToInt32(tokens_n[0]); | |
var columns = Convert.ToInt32(tokens_n[1]); | |
var colorGrid = new char[rows, columns]; | |
for (var row = 0; row < rows; row++) | |
{ | |
var column = 0; | |
var line = gameSetup[currLine++]; | |
foreach (var entry in line) | |
{ | |
colorGrid[row, column++] = entry; | |
} | |
} | |
var algo = new ConnectedColor(); | |
Console.WriteLine("---new run---"); | |
algo.printColorGrid(colorGrid); | |
var colorCount = algo.findColorConnections(rows, columns, colorGrid); | |
foreach (var result in colorCount) | |
{ | |
if ($"{result.Key}={result.Value}" != answers[answerP++]) | |
Debugger.Break(); | |
Console.WriteLine($"{result.Key}: {result.Value}"); | |
} | |
} | |
private Dictionary<char, int> findColorConnections(int rows, int columns, char[,] colorGrid) | |
{ | |
var visited = new bool[rows, columns]; | |
var colorCount = new Dictionary<char, int>(); | |
for (var row = 0; row < rows; row++) | |
{ | |
for (var column = 0; column < columns; column++) | |
{ | |
var color = colorGrid[row, column]; | |
var connectedCount = followColor(color, row, column, colorGrid, visited); | |
if (colorCount.ContainsKey(color)) | |
{ | |
if (colorCount[color] < connectedCount) | |
colorCount[color] = connectedCount; | |
} | |
else | |
colorCount[color] = connectedCount; | |
} | |
} | |
return colorCount; | |
} | |
private int followColor(char color, int row, int column, char[,] colorGrid, bool[,] visited) | |
{ | |
if (!isValidIndex(row, column, colorGrid) || visited[row, column] || colorGrid[row, column] != color) | |
return 0; | |
visited[row, column] = true; | |
//printVisited(visited); | |
// x 1 x | |
// 2 . 3 | |
// x 4 x | |
return 1 | |
/* 1 */ + followColor(color, row - 1, column, colorGrid, visited) | |
/* 2 */ + followColor(color, row, column - 1, colorGrid, visited) | |
/* 3 */ + followColor(color, row, column + 1, colorGrid, visited) | |
/* 4 */ + followColor(color, row + 1, column, colorGrid, visited); | |
} | |
private bool isValidIndex(int row, int column, char[,] colorGrid) | |
{ | |
return row >= 0 && column >= 0 && row < colorGrid.GetLength(0) && column < colorGrid.GetLength(1); | |
} | |
private void printVisited(bool[,] visited) | |
{ | |
Console.WriteLine("---"); | |
for (var row = 0; row < visited.GetLength(0); row++) | |
{ | |
for (var col = 0; col < visited.GetLength(1); col++) | |
{ | |
Console.Write(visited[row, col] ? '.' : ' '); | |
} | |
Console.WriteLine(); | |
} | |
} | |
private void printColorGrid(char[,] colorGrid) | |
{ | |
for (var row = 0; row < colorGrid.GetLength(0); row++) | |
{ | |
for (var col = 0; col < colorGrid.GetLength(1); col++) | |
{ | |
Console.Write(colorGrid[row, col]); | |
} | |
Console.WriteLine(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Answer for the problem at https://www.youtube.com/watch?v=IWvbPIYQPFM&ab_channel=TechLead