Tap here to switch tabs
Problem
Submissions

Problem: Minimum Initial Strength to Defeat All Monsters

Try to solve the Minimum Initial Strength to Defeat All Monsters problem.

Statement

You are given an integer array monsters and a 2D integer array boosts.

You must defeat all monsters in sequence from index 00 to monsters.length −1- 1.

Each element in boosts is given as [l_i, r_i, v_i], which grants an additional bonus strength of v_i at every index kk satisfying l_i ≤k≤\leq k \leq r_i. If multiple boosts cover the same index, their values accumulate.

Starting with an initial strength, to defeat the monster at index ii:

  1. Your effective strength at index ii, defined as the sum of your current strength and the total bonus strength at index ii, must be greater than or equal to monsters[i].

  2. After defeating the monster, your current strength decreases by monsters[i], but it cannot drop below 00. That is, your updated strength becomes max⁡(0,current strength−\max(0, \text{current strength} -monsters[i])).

Return the minimum initial strength required to defeat all monsters in order.

Constraints:

  • 1≤1 \leq monsters.length ≤5×104\leq 5 \times 10^4

  • 1≤1 \leq monsters[i] ≤109\leq 10^9

  • 0≤0 \leq boosts.length ≤5×104\leq 5 \times 10^4

  • boosts[i] == [l_i, r_i, v_i]

  • 0≤0 \leq l_i ≤\leq r_i << monsters.length

  • 1≤1 \leq v_i ≤109\leq 10^9

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Initial Strength to Defeat All Monsters

Try to solve the Minimum Initial Strength to Defeat All Monsters problem.

Statement

You are given an integer array monsters and a 2D integer array boosts.

You must defeat all monsters in sequence from index 00 to monsters.length −1- 1.

Each element in boosts is given as [l_i, r_i, v_i], which grants an additional bonus strength of v_i at every index kk satisfying l_i ≤k≤\leq k \leq r_i. If multiple boosts cover the same index, their values accumulate.

Starting with an initial strength, to defeat the monster at index ii:

  1. Your effective strength at index ii, defined as the sum of your current strength and the total bonus strength at index ii, must be greater than or equal to monsters[i].

  2. After defeating the monster, your current strength decreases by monsters[i], but it cannot drop below 00. That is, your updated strength becomes max⁡(0,current strength−\max(0, \text{current strength} -monsters[i])).

Return the minimum initial strength required to defeat all monsters in order.

Constraints:

  • 1≤1 \leq monsters.length ≤5×104\leq 5 \times 10^4

  • 1≤1 \leq monsters[i] ≤109\leq 10^9

  • 0≤0 \leq boosts.length ≤5×104\leq 5 \times 10^4

  • boosts[i] == [l_i, r_i, v_i]

  • 0≤0 \leq l_i ≤\leq r_i << monsters.length

  • 1≤1 \leq v_i ≤109\leq 10^9