Minimum Time For K Virus Variants to Spread - Problem
Virus Spread Simulation
Imagine an infinite 2D grid where
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
This is essentially finding the point that minimizes the sum of Manhattan distances to the
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.
Visualization
Tap to expand
Understanding the Visualization
1
Initial State
Virus variants positioned at given coordinates on day 0
2
Diamond Expansion
Each virus spreads in diamond pattern (Manhattan distance) daily
3
Coverage Analysis
Find intersections of k diamond-shaped coverage areas
4
Optimal Point
Identify point where k variants converge in minimum time
Key Takeaway
🎯 Key Insight: Transform the problem from finding optimal meeting points to geometric intersection of Manhattan distance diamonds. Binary search on time combined with coordinate transformation makes this efficient.
Time & Space Complexity
Time Complexity
O(n² log(max_coord))
Binary search O(log max_coord) iterations, each checking O(n²) virus pair intersections
⚠ Quadratic Growth
Space Complexity
O(1)
Only need constant space for calculations
✓ Linear Space
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)
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code