← Back to challenges

Deadly Virus

JavaScriptHardalgorithmsarrayslogiclanguage_fundamentals

Instructions

Mubashir needs your help to identify the spread of a deadly virus. He can provide you with the following parameters:

  • A two-dimensional array persons, containing affected persons 'V' and unaffected persons 'P'.
  • Number of hours n, each infected person is spreading the virus to one person up, down, left and right each hour.

Your function should return the updated array containing affected and unaffected persons after n hours.

Examples

persons = [
  ["P", "P", "P", "P", "P"],
  ["V", "P", "P", "P", "P"],
  ["P", "P", "P", "P", "P"],
  ["P", "P", "P", "P", "P"],
  ["P", "P", "P", "P", "P"]
]

deadlyVirus(persons, 0) ➞ [
  ["P", "P", "P", "P", "P"],
  ["V", "P", "P", "P", "P"],
  ["P", "P", "P", "P", "P"],
  ["P", "P", "P", "P", "P"],
  ["P", "P", "P", "P", "P"]
]

deadlyVirus(persons, 1) ➞ [
  ["V", "P", "P", "P", "P"],
  ["V", "V", "P", "P", "P"],
  ["V", "P", "P", "P", "P"],
  ["P", "P", "P", "P", "P"],
  ["P", "P", "P", "P", "P"]
]

deadlyVirus(persons, 2) ➞ [
  ["V", "V", "P", "P", "P"],
  ["V", "V", "V", "P", "P"],
  ["V", "V", "P", "P", "P"],
  ["V", "P", "P", "P", "P"],
  ["P", "P", "P", "P", "P"]
]

Notes

N/A

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.