FastPrepColored Cloud Statistics in a Ragged Grid

Colored Cloud Statistics in a Ragged Grid

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

You receive a possibly ragged picture as an array of strings grid. Rows may have different lengths. Character '0' is sky, and each digit from '1' through '9' is a cloud color.

A cloud is a maximal group of cells with the same nonzero color connected horizontally or vertically. Missing positions beyond the end of a shorter row are sky.

Return a String[] with one entry "color|cloudCount" for every color present, ordered by increasing color digit. Append "MAX|largestCloudSize" as the final entry. If the picture has no cloud cells, return only "MAX|0".

Function

analyzeColoredClouds(grid: String[]) → String[]

Examples

Example 1

grid = ["1102","11022","003"]return = ["1|1","2|1","3|1","MAX|4"]

The four color-1 cells form one cloud, the three color-2 cells form one cloud, and the single color-3 cell forms one cloud. The largest size is 4.

Example 2

grid = ["101","1","1011"]return = ["1|3","MAX|3"]

Ragged-row boundaries separate three color-1 clouds. The lower-right cloud has size 3.

Example 3

grid = ["000","","00"]return = ["MAX|0"]

No row contains a colored cell.

Constraints

  • 1 <= grid.length <= 300.
  • 0 <= grid[i].length <= 300; row lengths may differ.
  • The total number of cells across all rows is at most 90000.
  • Every character is a digit from '0' through '9'.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String[] analyzeColoredClouds(String[] grid) {
    // Write your code here.
}
grid["1102","11022","003"]
expected["1|1", "2|1", "3|1", "MAX|4"]
Checking account…