Problem Β· Matrix

Get Maximum Greyness πŸ‡

Learn this problem
● EasyAmazonINTERNOA
See Amazon hiring insights

Problem statement

Several satellites provide observational black and white images which are stored in data centers at Amazon Web Services (AWS).

A black and white image is composed of pixels and is represented as an n x m grid of cells. Each cell has a value of 0 or 1, where 0 represents a white pixel and 1 represents a black pixel. The grayness of a cell (i, j) is affected by the pixel values in the ith row and the jth column. More formally, the grayness of the cell (i, j) is the difference between the number of black pixels in the ith row and the jth column and the number of white pixels in the ith row and the jth column.

Find the maximum grayness among all the cells of the grid.

Note: The value of cell (i, j) is counted both in the ith row and in the jth column.

Function

getMaxGrayness(pixels: String[]) β†’ int

Complete the function getMaximumGreyness in the editor.

GetMaximumGreyness has the following parameter:

  • string pixels[n]: a grid of pixels, where the ithstring consists of m characters and represents the ithrow of pixels.
  • Returns

  • int: the max greyness of the grid of pixels.
  • *** πŸ‘‘ Credit to kcho πŸ‘‘ ***

    Examples

    Example 1

    pixels = ["1010", "0101", "1010"]return = 1
    The n x m = 3 x 4 grid of pixels looks like this: 1 0 1 0
    0 1 0 1
    1 0 1 0 The grayness of each cell is: 1 -1 1 -1
    1 -1 1 -1
    1 -1 1 -1 The maximum achievable grayness is 1.

    Example 2

    pixels = ["011", "101", "001"]return = 4
    The 3 x 3 grid of pixels looks like this: 0 1 1
    1 0 1
    0 0 1 The grayness of each cell is: 0 0 4
    0 0 4
    -2 -2 2 The maximum achievable grayness is 4.

    Example 3

    pixels = ["101", "001", "110"]return = 2
    Example 3 illustration
    The 3 x 3 grid of pixels looks like this: 1 0 1
    0 0 1
    1 1 0 The greyness of the cell (1, 1) is calculated as the image above πŸ‘† The greyness of each cell is: 2 0 2
    0 -2 0
    2 0 2 The maximum achievable greyness is 2.

    Constraints

    • 1 ≀ n, m ≀ 1000
    • pixels[i][j] = '0' or '1' for all 0 ≀ i < n and 0 ≀ j < m

    More Amazon problems

    drafts saved locally
    public int getMaxGrayness(String[] pixels) {
        // write your code here
    }
    
    pixels["1010", "0101", "1010"]
    expected1
    checking account