C Language, O(M *N) time, O(1) space, Do not modify the array


  • 0
    G

    the number of black lonely pixels just in the twice situtation
    1) Suppose variable x is the number of column which just have one 'B',
    2) Variable y is the number of row, the row just have one 'B',
    the answer of this question is get the minimum between x and y

    W,W,W,W,W
    W,B,W,W,W
    W,W,B,W,W
    W,W,W,W,B
    W,B,W,W,W

    x = 2
    y = 3
    the result is min(2, 3)

    int findLonelyPixel(char** picture, int pictureRowSize, int pictureColSize) {
    	if (NULL == picture || 0 == (pictureRowSize * pictureColSize))
    		return 0;
    
    	int x = 0, y = 0, n = 0;
    	for (int i = 0; i < pictureRowSize; i++, n = 0) {
    		for (int j = 0; j < pictureColSize; j++)
    			n += (picture[i][j] == 'B' ? 1 : 0);
    		x += (1 == n ? 1 : 0);
    	}
    	for (int j = 0; j < pictureColSize; j++, n = 0) {
    		for (int i = 0; i < pictureRowSize; i++)
    			n += (picture[i][j] == 'B' ? 1 : 0);
    		y += (1 == n ? 1 : 0);
    	}
    
    	return x > y ? y : x;
    }
    

  • 0
    J

    This solution is wrong. Try this test case: ["BWW","BWW","BWB"], the expected answer is 0, but your answer is 1.


  • 0
    G

    thx, I will review my code


Log in to reply
 

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