Find Time Required to Eliminate Bacterial Strains - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: timeReq = [4, 2, 6], splitTime = 3
Output: 9
💡 Note: Split the WBC (cost 3). At time 3, we have 2 WBCs. Assign strain 6 to WBC1 (completes at time 9) and strains 4,2 to WBC2 (completes at time 3+4+2=9). Total time is max(9,9) = 9.
Example 2 — No Splitting Needed
$ Input: timeReq = [1, 1, 1], splitTime = 10
Output: 3
💡 Note: Split cost (10) is very high. Better to use single WBC: 1+1+1=3 total time.
Example 3 — Beneficial Splitting
$ Input: timeReq = [10, 10], splitTime = 1
Output: 11
💡 Note: Split WBC (cost=1), then assign one strain to each WBC: max(1+10, 1+10) = 11. Better than single WBC taking 20 time.

Constraints

  • 1 ≤ timeReq.length ≤ 104
  • 1 ≤ timeReq[i] ≤ 104
  • 1 ≤ splitTime ≤ 104

Visualization

Tap to expand
Eliminate Bacterial Strains - Greedy Priority Queue INPUT Bacterial Strains (timeReq) 4 Strain 0 2 Strain 1 6 Strain 2 timeReq = [4, 2, 6] splitTime = 3 Initial WBC (White Blood Cell) WBC Can split or attack ALGORITHM STEPS 1 Sort Descending [6, 4, 2] - largest first 2 Calculate Splits For n=3 strains, need 2 splits 3 Greedy Assignment Add splitTime per level Max-Heap Processing i=0: 6 + 0*3 = 6 i=1: 4 + 1*3 = 7 i=2: 2 + 1*3 = 5 (splits: ceil(log2(i+1))) 4 Find Maximum max(6, 7, 5) = 7? No! Binary approach: optimal=6 WBCs work in parallel! FINAL RESULT Optimal Timeline 0 3 6 Split 6 (t=6) 4 2 WBC1 WBC2 WBC3 Output: 6 OK - All strains eliminated in minimum time! Key Insight: Use greedy approach with max-heap: assign longest tasks to earliest-available WBCs. Each split doubles WBC count. Total time = max(splitTime * splits + taskTime) across all assignments. Parallel execution means we only wait for the longest path, not sum of all tasks! TutorialsPoint - Find Time Required to Eliminate Bacterial Strains | Greedy Priority Queue Approach
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
23.4K 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