Minimum Time For K Virus Variants to Spread - Problem
Virus Spread Simulation

Imagine an infinite 2D grid where n unique virus variants begin spreading simultaneously from different origin points. You are given a 2D array points, where points[i] = [xi, yi] represents the initial location of virus variant i on day 0.

Key mechanics:
• Each day, every infected cell spreads its virus(es) to all 4 neighboring cells (up, down, left, right)
• Multiple virus variants can coexist in the same cell without interfering
• Each variant spreads independently following Manhattan distance rules

Goal: Given an integer k, find the minimum number of days required for any single point in the grid to contain at least k different virus variants.

This is essentially finding the point that minimizes the sum of Manhattan distances to the k closest virus origins.

Input & Output

example_1.py — Basic case
$ Input: points = [[0,0], [1,1], [2,0]], k = 2
Output: 1
💡 Note: On day 1, virus from (0,0) reaches (1,0), virus from (1,1) reaches (1,0). Point (1,0) contains 2 variants after 1 day.
example_2.py — Same starting points
$ Input: points = [[0,0], [0,0], [1,1]], k = 3
Output: 1
💡 Note: Two variants start at (0,0), one at (1,1). Point (0,0) already has 2 variants on day 0, and gets the third variant from (1,1) on day 1.
example_3.py — Impossible case
$ Input: points = [[0,0], [1,1]], k = 3
Output: -1
💡 Note: Only 2 virus variants exist, but we need 3. This is impossible.

Constraints

  • 1 ≤ n ≤ 100
  • points[i].length == 2
  • -106 ≤ xi, yi ≤ 106
  • 1 ≤ k ≤ n
  • All virus variants are distinct (even if starting at same point)

Visualization

Tap to expand
Virus Spread Simulation INPUT V0 V1 V2 (0,0) (1,1) (2,0) points = [[0,0],[1,1],[2,0]] k = 2 n = 3 virus variants Manhattan Distance: |x1-x2| + |y1-y2| Spread: 4 directions/day ALGORITHM STEPS 1 Binary Search on Days Search range: 0 to max_dist 2 For Each Day d Check if k variants meet 3 Diamond Intersection Each virus covers diamond 4 Find Minimum Days k closest points overlap Day 1 spread dist(V0,V1) = |1-0|+|1-0| = 2 FINAL RESULT 2+ Point (1,0) has 2 variants after Day 1 V0 to (1,0): |1|+|0| = 1 V2 to (1,0): |1|+|0| = 1 Both reach in 1 day! Output: 1 Minimum Days Key Insight: After d days, virus at (x,y) covers all points where Manhattan distance <= d (diamond shape). For k=2: Find point where 2 closest virus origins have minimum max-distance. Binary search finds optimal day. TutorialsPoint - Minimum Time For K Virus Variants to Spread | Optimal Solution
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
24.6K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen