Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6420 |

@blackshadows | 6376 |

@mb1973 | 5594 |

@Quandray | 5231 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 4510 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@marius_valentin_dragoi | 3522 |

@sushant_a | 3459 |

@verma_ji | 3412 |

Complete Leaderboard | |

Handle | Score |

@codersgroup18 | 783 |

@Manikanta punnam | 605 |

@sriramgoparaju99 | 582 |

@rdakka | 534 |

@praveenbgp6 | 529 |

@prankursharma31 | 518 |

@yashkaril4 | 517 |

@purohitmn02 | 467 |

@sonamnigam1999 | 443 |

@sonamkumari63928 | 441 |

@shubhamstudent5 | 433 |

Complete Leaderboard |

Aterp is the head nurse at a city hospital. City hospital contains R*C number of wards and the structure of a hospital is in the form of a 2-D matrix.

Given a matrix of dimension **R*****C** where each cell in the matrix can have values 0, 1, or 2 which has the following meaning:

**0**: Empty ward

**1**: Cells have uninfected patients

**2**: Cells have infected patients

An infected patient at ward [i,j] can infect other uninfected patient at indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1] (**up**, **down**, **left** and **right**) in unit time. Help Aterp determine the minimum units of time after which there won't remain any uninfected patient i.e all patients would be infected. If all patients are not infected after infinite units of time then simply return -1.

**Example 1:**

Input:3 5 2 1 0 2 1 1 0 1 2 1 1 0 0 2 1Output:2Explanation:

```
Patients at positions {0,0}, {0, 3}, {1, 3}
and {2, 3} will infect patient at {0, 1},
{1, 0},{0, 4}, {1, 2}, {1, 4}, {2, 4} during 1st
unit time. And, during 2nd unit time, patient at
{1, 0} will get infected and will infect patient
at {2, 0}. Hence, total 2 unit of time is
required to infect all patients.
```

**Example 2:**

Input:3 5 2 1 0 2 1 0 0 1 2 1 1 0 0 2 1Output:-1Explanation:All patients will not be infected.

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **helpaterp**() which takes a 2-D Matrix **hospital **as input parameter and returns the minimum units of time in which all patients will be infected or -1 if it is impossible.

**Expected Time Complexity:** O(R*C)

**Expected Auxiliary Space:** O(R*C)

**Constraints:**

1 ≤ R,C ≤ 1000

0 ≤ mat[i][j] ≤ 2

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Covid Spread

...