[LeetCode] Find Time Required to Eliminate Bacterial Strains

3506. Find Time Required to Eliminate Bacterial Strains

You are given an integer array timeReq and an integer splitTime.

In the microscopic world of the human body, the immune system faces an extraordinary challenge: combatting a rapidly multiplying bacterial colony that threatens the body’s survival.

Initially, only one white blood cell (WBC) is deployed to eliminate the bacteria. However, the lone WBC quickly realizes it cannot keep up with the bacterial growth rate.

The WBC devises a clever strategy to fight the bacteria:

  • The ith bacterial strain takes timeReq[i] units of time to be eliminated.
  • A single WBC can eliminate only one bacterial strain. Afterwards, the WBC is exhausted and cannot perform any other tasks.
  • A WBC can split itself into two WBCs, but this requires splitTime units of time. Once split, the two WBCs can work in parallel on eliminating the bacteria.
  • Only one WBC can work on a single bacterial strain. Multiple WBCs cannot attack one strain in parallel.

You must determine the minimum time required to eliminate all the bacterial strains.

Note that the bacterial strains can be eliminated in any order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
bool helper(vector<int>& A, long long k, long long target) {
long long cnt = 1, idx = 0, time = 0, n = A.size();
while(time <= target and idx < n and cnt) {
if(cnt >= n - idx) {
time += A[idx];
idx = n;
break;
}
while(idx < n and cnt and target - A[idx] < time + k) {
cnt--;
idx++;
}
cnt *= 2;
time += k;
}
return time <= target and idx == n;
}
public:
long long minEliminationTime(vector<int>& timeReq, int splitTime) {
sort(rbegin(timeReq), rend(timeReq));
long long l = 0, r = LLONG_MAX, res = r;
while(l <= r) {
long long m = l + (r - l) / 2;
bool ok = helper(timeReq, splitTime, m);
if(ok) {
r = m - 1;
res = m;
} else l = m + 1;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/05/18/PS/LeetCode/find-time-required-to-eliminate-bacterial-strains/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.