Java O(nm) solution that is beating 100%


  • 0
    K

    The idea is to keep track of number of black pixels in each row and column.

    public class Solution {
        public int findLonelyPixel(char[][] picture) {
            int n = picture.length;
            int m = picture[0].length;
            int[] rCount = new int[n];
            int[] cCount = new int[m];
            for(int i = 0; i < n; ++i)
            {
                for(int j=0; j < m; ++j)
                {
                    if(picture[i][j]=='B')
                    {
                        ++rCount[i];
                        ++cCount[j];
                    }
                }
            }
            int ans = 0;
            for(int i = 0; i < n; ++i)
            {
                if(rCount[i]==1)
                {
                    for(int j=0; j < m; ++j)
                    {
                        if(cCount[j] == 1 &&picture[i][j]=='B')
                        {
                            ++ans;
                        }
                    }
                }
            }
            return ans;
        }
    }
    

Log in to reply
 

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