Skip to main content
Back to problems
#3506
Hard Algorithms

Find time required to eliminate bacterial strains

Array Math Greedy Heap (Priority Queue)
61.3% acceptance
Mar 31, 2026
4
1
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.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn min_elimination_time(time_req: Vec<i32>, split_time: i32) -> i64 {
    use std::collections::BinaryHeap;
    use std::cmp::Reverse;
    
    let st = split_time as i64;
    let mut heap: BinaryHeap<Reverse<i64>> = time_req.iter().map(|&x| Reverse(x as i64)).collect();
    
    while heap.len() > 1 {
      let Reverse(_a) = heap.pop().unwrap();
      let Reverse(b) = heap.pop().unwrap();
      heap.push(Reverse(st + b));
    }
    
    heap.pop().unwrap().0
  }
}