C# Simple Solution using Dictionary


  • 0
    K

    -Loop each row and column of 'B'
    -count row occurrences
    -Repeat above step for columns of 'B'
    -In the second pass count those rows and columns of 'B' which are single (equal to 1)

       public int FindLonelyPixel(char[,] picture)
            {
                if (picture == null) return 0;
    
                int count = 0;
                Dictionary<int, int> rows = new Dictionary<int, int>();
                Dictionary<int, int> columns = new Dictionary<int, int>();
                int c = -1;
                int r = -1;
                for (int i = 0; i < picture.GetLength(0); i++)
                {
                    for (int j = 0; j < picture.GetLength(1); j++)
                    {
                        if (picture[i, j] == 'W') continue;
                        if (!rows.TryGetValue(i, out r)) rows.Add(i, 0);
                        rows[i]++;
                        if (!columns.TryGetValue(j, out c)) columns.Add(j, 0);
                        columns[j]++;
                    }
                }
    
                for (int i = 0; i < picture.GetLength(0); i++)
                {
                    for (int j = 0; j < picture.GetLength(1); j++)
                    {
                       if (picture[i, j] == 'W') continue;
                       if (rows.TryGetValue(i, out r) && (columns.TryGetValue(j, out c)) && r == 1 && c == 1) count++;
                    }
                }
                return count;
            }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.