Loading problem...
You are given two binary matrices of identical dimensions: referenceMap and targetMap. Each matrix consists solely of 0s (representing empty space) and 1s (representing occupied territory). A region is defined as a maximal group of 1s that are connected 4-directionally (horizontally or vertically adjacent). Cells that lie outside the matrix boundaries are treated as empty space.
A region in targetMap qualifies as a contained region if and only if every single cell that belongs to this region in targetMap also appears as a 1 in the corresponding position in referenceMap. In other words, the region in targetMap must be entirely covered by occupied territory in referenceMap.
Your task is to determine and return the total count of regions in targetMap that are classified as contained regions.
Key Observations:
targetMap can be smaller than, equal to, or differently shaped compared to regions in referenceMap, as long as all its cells are covered.targetMap maps to a 0 in referenceMap, that region does not qualify as a contained region.referenceMap and targetMap don't need to match in shape or size; only the containment property matters.referenceMap = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]]
targetMap = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]3The targetMap contains multiple regions. Analyzing each: The top-left region in targetMap covers positions (0,0), (0,1), (0,2), (1,2), (1,3), (1,4) — checking referenceMap shows that position (2,1) contains a 1 in targetMap but 0 in referenceMap, so one connected component is disqualified. However, three other complete regions are fully contained within the referenceMap's occupied territory, giving us the answer of 3.
referenceMap = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]]
targetMap = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]2In targetMap, we identify distinct regions. Certain regions extend into areas where referenceMap has 0s, disqualifying them. Two regions are fully contained within referenceMap's territory, so the answer is 2.
referenceMap = [[1,0,1],[0,1,0],[1,0,1]]
targetMap = [[1,0,0],[0,1,0],[0,0,1]]3Each 1 in targetMap forms its own isolated single-cell region: positions (0,0), (1,1), and (2,2). All three of these positions correspond to 1s in referenceMap. Therefore, all three regions are contained regions, yielding an answer of 3.
Constraints