Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given an integer power and two integer arrays damage and health, both having length n.
Bob has n enemies, where enemy i will deal Bob damage[i] points of damage per second while they are alive (i.e. health[i] > 0).
Every second, after the enemies deal damage to Bob, he chooses one of the enemies that is still alive and deals power points of damage to them.
Determine the minimum total amount of damage points that will be dealt to Bob before all n enemies are dead.
Example 1:
Input: power = 4, damage = [1,2,3,4], health = [4,5,6,8]
Output: 39
Explanation:
10 + 10 = 20 points.6 + 6 = 12 points.3 points.2 + 2 = 4 points.Example 2:
Input: power = 1, damage = [1,1,1,1], health = [1,2,3,4]
Output: 20
Explanation:
4 points.3 + 3 = 6 points.2 + 2 + 2 = 6 points.1 + 1 + 1 + 1 = 4 points.Example 3:
Input: power = 8, damage = [40], health = [59]
Output: 320
Constraints:
1 <= power <= 1041 <= n == damage.length == health.length <= 1051 <= damage[i], health[i] <= 104Problem summary: You are given an integer power and two integer arrays damage and health, both having length n. Bob has n enemies, where enemy i will deal Bob damage[i] points of damage per second while they are alive (i.e. health[i] > 0). Every second, after the enemies deal damage to Bob, he chooses one of the enemies that is still alive and deals power points of damage to them. Determine the minimum total amount of damage points that will be dealt to Bob before all n enemies are dead.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
4 [1,2,3,4] [4,5,6,8]
1 [1,1,1,1] [1,2,3,4]
8 [40] [59]
minimum-time-to-complete-trips)minimum-penalty-for-a-shop)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3273: Minimum Amount of Damage Dealt to Bob
class Enemy {
public int damage;
public int timeTakenDown;
public Enemy(int damage, int timeTakenDown) {
this.damage = damage;
this.timeTakenDown = timeTakenDown;
}
}
class Solution {
public long minDamage(int power, int[] damage, int[] health) {
long ans = 0;
long sumDamage = Arrays.stream(damage).asLongStream().sum();
Enemy[] enemies = new Enemy[damage.length];
for (int i = 0; i < damage.length; ++i)
enemies[i] = new Enemy(damage[i], (health[i] + power - 1) / power);
// It's better to take down the enemy i first if the damage dealt of taking
// down i first is less than the damage dealt of taking down j first. So,
// damage[i] * t[i] + (t[i] + t[j]) * damage[j] <
// damage[j] * t[j] + (t[i] + t[j]) * damage[i]
// => damage[i] * t[i] + damage[j] * t[i] + damage[j] * t[j] <
// damage[j] * t[j] + damage[i] * t[j] + damage[i] * t[i]
// => damage[j] * t[i] < damage[i] * t[j]
// => damage[j] / t[j] < damage[i] / t[i]
Arrays.sort(enemies,
(a, b)
-> Double.compare((double) b.damage / b.timeTakenDown,
(double) a.damage / a.timeTakenDown));
for (final Enemy enemy : enemies) {
ans += sumDamage * enemy.timeTakenDown;
sumDamage -= enemy.damage;
}
return ans;
}
}
// Accepted solution for LeetCode #3273: Minimum Amount of Damage Dealt to Bob
package main
import "slices"
// https://space.bilibili.com/206214
func minDamage(power int, damage, health []int) (ans int64) {
type pair struct{ k, d int }
a := make([]pair, len(health))
for i, h := range health {
a[i] = pair{(h-1)/power + 1, damage[i]}
}
slices.SortFunc(a, func(p, q pair) int { return p.k*q.d - q.k*p.d })
s := 0
for _, p := range a {
s += p.k
ans += int64(s) * int64(p.d)
}
return
}
# Accepted solution for LeetCode #3273: Minimum Amount of Damage Dealt to Bob
from dataclasses import dataclass
@dataclass(frozen=True)
class Enemy:
damage: int
timeTakenDown: int
class Solution:
def minDamage(self, power: int, damage: list[int], health: list[int]) -> int:
ans = 0
sumDamage = sum(damage)
enemies = [Enemy(d, (h + power - 1) // power)
for d, h in zip(damage, health)]
# It's better to take down the enemy i first if the damage dealt of taking
# down i first is less than the damage dealt of taking down j first. So,
# damage[i] * t[i] + (t[i] + t[j]) * damage[j] <
# damage[j] * t[j] + (t[i] + t[j]) * damage[i]
# => damage[i] * t[i] + damage[j] * t[i] + damage[j] * t[j] <
# damage[j] * t[j] + damage[i] * t[j] + damage[i] * t[i]
# => damage[j] * t[i] < damage[i] * t[j]
# => damage[j] / t[j] < damage[i] / t[i]
enemies.sort(key=lambda x: -x.damage / x.timeTakenDown)
for enemy in enemies:
ans += sumDamage * enemy.timeTakenDown
sumDamage -= enemy.damage
return ans
// Accepted solution for LeetCode #3273: Minimum Amount of Damage Dealt to Bob
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3273: Minimum Amount of Damage Dealt to Bob
// class Enemy {
// public int damage;
// public int timeTakenDown;
// public Enemy(int damage, int timeTakenDown) {
// this.damage = damage;
// this.timeTakenDown = timeTakenDown;
// }
// }
//
// class Solution {
// public long minDamage(int power, int[] damage, int[] health) {
// long ans = 0;
// long sumDamage = Arrays.stream(damage).asLongStream().sum();
// Enemy[] enemies = new Enemy[damage.length];
//
// for (int i = 0; i < damage.length; ++i)
// enemies[i] = new Enemy(damage[i], (health[i] + power - 1) / power);
//
// // It's better to take down the enemy i first if the damage dealt of taking
// // down i first is less than the damage dealt of taking down j first. So,
// // damage[i] * t[i] + (t[i] + t[j]) * damage[j] <
// // damage[j] * t[j] + (t[i] + t[j]) * damage[i]
// // => damage[i] * t[i] + damage[j] * t[i] + damage[j] * t[j] <
// // damage[j] * t[j] + damage[i] * t[j] + damage[i] * t[i]
// // => damage[j] * t[i] < damage[i] * t[j]
// // => damage[j] / t[j] < damage[i] / t[i]
// Arrays.sort(enemies,
// (a, b)
// -> Double.compare((double) b.damage / b.timeTakenDown,
// (double) a.damage / a.timeTakenDown));
//
// for (final Enemy enemy : enemies) {
// ans += sumDamage * enemy.timeTakenDown;
// sumDamage -= enemy.damage;
// }
//
// return ans;
// }
// }
// Accepted solution for LeetCode #3273: Minimum Amount of Damage Dealt to Bob
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3273: Minimum Amount of Damage Dealt to Bob
// class Enemy {
// public int damage;
// public int timeTakenDown;
// public Enemy(int damage, int timeTakenDown) {
// this.damage = damage;
// this.timeTakenDown = timeTakenDown;
// }
// }
//
// class Solution {
// public long minDamage(int power, int[] damage, int[] health) {
// long ans = 0;
// long sumDamage = Arrays.stream(damage).asLongStream().sum();
// Enemy[] enemies = new Enemy[damage.length];
//
// for (int i = 0; i < damage.length; ++i)
// enemies[i] = new Enemy(damage[i], (health[i] + power - 1) / power);
//
// // It's better to take down the enemy i first if the damage dealt of taking
// // down i first is less than the damage dealt of taking down j first. So,
// // damage[i] * t[i] + (t[i] + t[j]) * damage[j] <
// // damage[j] * t[j] + (t[i] + t[j]) * damage[i]
// // => damage[i] * t[i] + damage[j] * t[i] + damage[j] * t[j] <
// // damage[j] * t[j] + damage[i] * t[j] + damage[i] * t[i]
// // => damage[j] * t[i] < damage[i] * t[j]
// // => damage[j] / t[j] < damage[i] / t[i]
// Arrays.sort(enemies,
// (a, b)
// -> Double.compare((double) b.damage / b.timeTakenDown,
// (double) a.damage / a.timeTakenDown));
//
// for (final Enemy enemy : enemies) {
// ans += sumDamage * enemy.timeTakenDown;
// sumDamage -= enemy.damage;
// }
//
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.