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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are entering a competition, and are given two positive integers initialEnergy and initialExperience denoting your initial energy and initial experience respectively.
You are also given two 0-indexed integer arrays energy and experience, both of length n.
You will face n opponents in order. The energy and experience of the ith opponent is denoted by energy[i] and experience[i] respectively. When you face an opponent, you need to have both strictly greater experience and energy to defeat them and move to the next opponent if available.
Defeating the ith opponent increases your experience by experience[i], but decreases your energy by energy[i].
Before starting the competition, you can train for some number of hours. After each hour of training, you can either choose to increase your initial experience by one, or increase your initial energy by one.
Return the minimum number of training hours required to defeat all n opponents.
Example 1:
Input: initialEnergy = 5, initialExperience = 3, energy = [1,4,3,2], experience = [2,6,3,1] Output: 8 Explanation: You can increase your energy to 11 after 6 hours of training, and your experience to 5 after 2 hours of training. You face the opponents in the following order: - You have more energy and experience than the 0th opponent so you win. Your energy becomes 11 - 1 = 10, and your experience becomes 5 + 2 = 7. - You have more energy and experience than the 1st opponent so you win. Your energy becomes 10 - 4 = 6, and your experience becomes 7 + 6 = 13. - You have more energy and experience than the 2nd opponent so you win. Your energy becomes 6 - 3 = 3, and your experience becomes 13 + 3 = 16. - You have more energy and experience than the 3rd opponent so you win. Your energy becomes 3 - 2 = 1, and your experience becomes 16 + 1 = 17. You did a total of 6 + 2 = 8 hours of training before the competition, so we return 8. It can be proven that no smaller answer exists.
Example 2:
Input: initialEnergy = 2, initialExperience = 4, energy = [1], experience = [3] Output: 0 Explanation: You do not need any additional energy or experience to win the competition, so we return 0.
Constraints:
n == energy.length == experience.length1 <= n <= 1001 <= initialEnergy, initialExperience, energy[i], experience[i] <= 100Problem summary: You are entering a competition, and are given two positive integers initialEnergy and initialExperience denoting your initial energy and initial experience respectively. You are also given two 0-indexed integer arrays energy and experience, both of length n. You will face n opponents in order. The energy and experience of the ith opponent is denoted by energy[i] and experience[i] respectively. When you face an opponent, you need to have both strictly greater experience and energy to defeat them and move to the next opponent if available. Defeating the ith opponent increases your experience by experience[i], but decreases your energy by energy[i]. Before starting the competition, you can train for some number of hours. After each hour of training, you can either choose to increase your initial experience by one, or increase your initial energy by one. Return the minimum number of
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
5 3 [1,4,3,2] [2,6,3,1]
2 4 [1] [3]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2383: Minimum Hours of Training to Win a Competition
class Solution {
public int minNumberOfHours(int x, int y, int[] energy, int[] experience) {
int ans = 0;
for (int i = 0; i < energy.length; ++i) {
int dx = energy[i], dy = experience[i];
if (x <= dx) {
ans += dx + 1 - x;
x = dx + 1;
}
if (y <= dy) {
ans += dy + 1 - y;
y = dy + 1;
}
x -= dx;
y += dy;
}
return ans;
}
}
// Accepted solution for LeetCode #2383: Minimum Hours of Training to Win a Competition
func minNumberOfHours(x int, y int, energy []int, experience []int) (ans int) {
for i, dx := range energy {
dy := experience[i]
if x <= dx {
ans += dx + 1 - x
x = dx + 1
}
if y <= dy {
ans += dy + 1 - y
y = dy + 1
}
x -= dx
y += dy
}
return
}
# Accepted solution for LeetCode #2383: Minimum Hours of Training to Win a Competition
class Solution:
def minNumberOfHours(
self, x: int, y: int, energy: List[int], experience: List[int]
) -> int:
ans = 0
for dx, dy in zip(energy, experience):
if x <= dx:
ans += dx + 1 - x
x = dx + 1
if y <= dy:
ans += dy + 1 - y
y = dy + 1
x -= dx
y += dy
return ans
// Accepted solution for LeetCode #2383: Minimum Hours of Training to Win a Competition
impl Solution {
pub fn min_number_of_hours(
mut x: i32,
mut y: i32,
energy: Vec<i32>,
experience: Vec<i32>,
) -> i32 {
let mut ans = 0;
for (&dx, &dy) in energy.iter().zip(experience.iter()) {
if x <= dx {
ans += dx + 1 - x;
x = dx + 1;
}
if y <= dy {
ans += dy + 1 - y;
y = dy + 1;
}
x -= dx;
y += dy;
}
ans
}
}
// Accepted solution for LeetCode #2383: Minimum Hours of Training to Win a Competition
function minNumberOfHours(x: number, y: number, energy: number[], experience: number[]): number {
let ans = 0;
for (let i = 0; i < energy.length; ++i) {
const [dx, dy] = [energy[i], experience[i]];
if (x <= dx) {
ans += dx + 1 - x;
x = dx + 1;
}
if (y <= dy) {
ans += dy + 1 - y;
y = dy + 1;
}
x -= dx;
y += dy;
}
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.