Skip to main content

Minimum Time For K Virus Variants to Spread - Solution & Explanation

HardPremiumFree on FleetCodeArrayMathBinary SearchGeometry4 min read
Practice this problem

Problem Statement

There are n unique virus variants in an infinite 2D grid. You are given a 2D array points, where points[i] = [xi, yi] represents a virus originating at (xi, yi) on day 0. Note that it is possible for multiple virus variants to originate at the same point.

Every day, each cell infected with a virus variant will spread the virus to all neighboring points in the four cardinal directions (i.e. up, down, left, and right). If a cell has multiple variants, all the variants will spread without interfering with each other.

Given an integer k, return the minimum integer number of days for any point to contain at least k of the unique virus variants.

 

Example 1:

Input: points = [[1,1],[6,1]], k = 2
Output: 3
Explanation: On day 3, points (3,1) and (4,1) will contain both virus variants. Note that these are not the only points that will contain both virus variants.

Example 2:

Input: points = [[3,3],[1,2],[9,2]], k = 2
Output: 2
Explanation: On day 2, points (1,3), (2,3), (2,2), and (3,2) will contain the first two viruses. Note that these are not the only points that will contain both virus variants.

Example 3:

Input: points = [[3,3],[1,2],[9,2]], k = 3
Output: 4
Explanation: On day 4, the point (5,2) will contain all 3 viruses. Note that this is not the only point that will contain all 3 virus variants.

 

Constraints:

  • n == points.length
  • 2 <= n <= 50
  • points[i].length == 2
  • 1 <= xi, yi <= 100
  • 2 <= k <= n

Approach Overview

Problem Overview: You are given coordinates of different virus variants on a grid. Each variant spreads one unit per second using Manhattan distance. The task is to compute the minimum time t such that there exists at least one grid cell infected by k different variants.

Approach 1: Brute Force Candidate Points (High Polynomial Time)

The Manhattan spread from each variant forms a diamond region. If you enumerate candidate intersection points formed by the boundaries of these diamonds, you can test whether at least k variants cover that point. For every candidate point, compute |x - xi| + |y - yi| for all variants and count how many are within time t. This approach directly checks geometric overlap but the number of candidate points grows quickly, leading to roughly O(n^3) time and O(1) extra space. It mainly serves as intuition for how the overlap region behaves.

Approach 2: Binary Search + Coordinate Transformation + Sweep Line (Optimal)

The key observation is that a Manhattan distance region |x - xi| + |y - yi| ≤ t becomes an axis‑aligned square after transforming coordinates: u = x + y and v = x - y. Each virus then covers a square defined by [ui - t, ui + t] and [vi - t, vi + t]. The problem becomes finding whether a point exists that lies inside at least k of these squares.

Binary search the answer on time t. For each candidate t, convert every diamond to its square representation and run a sweep line over the u axis. Add events for square entry and exit. While sweeping, maintain overlapping intervals on the v axis using a segment tree or coordinate-compressed difference structure. If any position on v reaches coverage ≥ k, a point exists where k variants meet.

This reduces the geometric search dramatically. The check runs in O(n log n) time with O(n) space, and binary search adds a log T factor where T is the coordinate range. The final complexity becomes O(n log n log T). The technique relies on ideas from geometry, binary search, and sweep-line style interval processing.

Recommended for interviews: The expected solution is the binary search combined with geometric transformation and sweep line. Brute force shows understanding of Manhattan regions, but the optimized approach demonstrates the ability to convert geometry constraints into interval overlap problems and apply efficient search techniques.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Candidate IntersectionO(n^3)O(1)Useful for understanding geometric overlap of Manhattan diamonds; impractical for large inputs
Binary Search + Sweep Line on Transformed CoordinatesO(n log n log T)O(n)General optimal solution when coordinates are large and you need to detect k overlapping regions efficiently

Video Solution

1956. Minimum Time For K Virus Variants to Spread (Leetcode Hard)Programming Live with Larry489 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Minimum Time For K Virus Variants to Spread easy or hard?
LeetCode classifies this problem as Hard. The challenge comes from recognizing the Manhattan-to-square coordinate transformation and combining binary search with sweep-line interval counting to detect k overlapping regions.
Minimum Time For K Virus Variants to Spread Python/Java solution
Implement binary search over time and a feasibility check that converts coordinates to (u, v). During the sweep line, maintain interval counts using a segment tree or ordered structure. The same algorithm works in Python, Java, C++, and Go with O(n log n log T) complexity.
What is the best approach for Minimum Time For K Virus Variants to Spread?
The most efficient approach uses binary search on time combined with a geometric transformation. Convert Manhattan diamonds into axis-aligned squares using u = x + y and v = x - y. Then apply a sweep line over one axis while tracking interval overlaps on the other. This detects whether at least k regions intersect in O(n log n) per check.
Is Minimum Time For K Virus Variants to Spread asked at Google/Amazon/Meta?
Problems combining binary search with geometric transformations and sweep-line interval counting are common in interviews at companies like Google and Meta. The pattern of converting Manhattan distance regions into axis-aligned ranges is especially popular in algorithmic geometry questions.
What data structure is used in Minimum Time For K Virus Variants to Spread?
The optimal solution typically uses a sweep line with a segment tree or coordinate-compressed difference array to track interval overlaps. Binary search controls the candidate time values, while geometry transformation simplifies Manhattan regions into squares.
What is the time complexity of Minimum Time For K Virus Variants to Spread?
The optimal solution runs in O(n log n log T) time, where n is the number of variants and T is the coordinate range searched during binary search. Each feasibility check uses a sweep line and interval structure that costs O(n log n). Space complexity is O(n).
How to solve Minimum Time For K Virus Variants to Spread in O(n log n log T)?
Binary search the minimum spreading time t. For each t, transform every Manhattan diamond |x-xi|+|y-yi|≤t into an axis-aligned square in rotated coordinates. Sweep across one dimension and maintain active intervals on the other using a segment tree or coordinate compression. If any point reaches coverage k, that time is feasible.

Ready to solve this problem?

Practice Minimum Time For K Virus Variants to Spread with our built-in code editor and test cases.

Practice on FleetCode