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: 6
💡 Note: Start with 1 WBC. Assign strain 6 (time=6), then strain 4 (time=4), then strain 2 to second WBC (time=6). Total time is max(6,4) after optimal splitting = 6.
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
WBC Bacterial Elimination: Split vs Assign StrategyBacterial Strains:[4, 2, 6]Split Cost:3Initial WBC:1WBC 1:Strain 6Split +3→ 2 WBCsWBC 2:Strain 4WBC 2:Strain 2Final completion times: WBC1=6, WBC2=max(3+4, 3+4+2)=9🎯 Optimal Time: max(6, 6) = 6
Understanding the Visualization
1
Input
Bacterial strains [4,2,6] and split cost 3
2
Strategy
Decide when to split WBCs vs assign strains directly
3
Output
Minimum time to eliminate all strains: 6
Key Takeaway
🎯 Key Insight: Balance split costs against parallel work benefits to minimize total completion time
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