Trains the technique from
LeetCode 749Contain VirusThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A basement floor plan is the grid isInfected, where isInfected[i][j] is 1 when the square is already mouldy and 0 when it is still clean. Two mouldy squares belong to the same patch when they share an edge; squares touching only at a corner are in different patches.
A contractor works in rounds. In a round:
A sealed patch is finished with: it never grows again, and it is never sealed again. A patch that threatens no clean square is left as it is, since it cannot grow and needs no panel.
If two or more patches threaten the same number of distinct clean squares, seal the one whose first square comes earliest when the grid is read row by row, left to right within a row.
Return the total number of panels fitted once the job is finished.
Example 1
All the mouldy squares share edges, so they form one patch, which threatens the clean squares at (1, 1) and (1, 3). Square (1, 1) is touched on four sides and square (1, 3) on three, so sealing costs 7 panels and the job ends.
Example 2
The single mouldy square threatens the four clean squares beside it and is sealed with one panel per shared edge, after which nothing can grow.
Example 3
The plan holds no clean square at all, so the one patch threatens nothing, no panel is fitted and the job is finished immediately.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def contain_virus(isInfected: list[list[int]]) -> int:public int containVirus(int[][] isInfected)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.