C# Solution, O(n) space


  • 0
    J
    public int FindLonelyPixel(char[,] picture) {
                int []row = new int[picture.GetLength(0)];
                int []col = new int[picture.GetLength(1)];
    
                for (var i=0;i< picture.GetLength(0);++i)
                {
                    for (var j=0;j<picture.GetLength(1);++j)
                    {
                        if (picture[i,j] == 'B')
                        {
                            row[i]++;
                            col[j]++;
                        }
                    }
                }
    
                int count = 0;
                for (var i=0;i<row.Length;++i)
                {
                    if (row[i] == 1)
                    {
                        for (var j = 0; j < picture.GetLength(1); ++j)
                        {
                            if (picture[i, j] == 'B')
                            {
                                if (col[j] == 1)
                                    count++;
                                break;
                            }
                        }
                    }
                }
                return count;
        }
    

Log in to reply
 

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