File: minimum-hours-of-training-to-win-a-competition/solution.py

Date: 2026-06-07

Time: 04:52

Purpose

This file solves LeetCode 2383: Minimum Hours of Training to Win a Competition. It computes the minimum number of training hours (each hour adds +1 to either energy or experience) needed so a player can defeat every opponent in sequence. The player must have strictly greater energy and experience than each opponent at the time of the fight.

Key Components

mintraininghours(initialEnergy, initialExperience, energy, experience) -> int

The single exported function. It splits the problem into two independent subproblems and sums the results:

Energy subproblem (line 22): Energy is consumed additively — every fight costs energy[i] and the player must survive all fights. This reduces to a single arithmetic check: total energy needed is sum(energy) + 1, and the training gap is max(0, sum(energy) + 1 - initialEnergy).

Experience subproblem (lines 25–31): Experience is gained after each win — the player gets experience[i] added after defeating opponent i. Because gains compound, the deficit must be resolved opponent-by-opponent via simulation. When curexp <= exp, the function computes the exact gap (exp + 1 - curexp), adds it to both the running total and the current experience, then adds the opponent's experience as the win reward.

Patterns

Dependencies

Imports: typing.List — used only for type annotations.

Imported by: minimum-hours-of-training-to-win-a-competition/test_solution.py (the "Imported By" list in the prompt is misleading — it lists all test files across the repo that import from their own solution.py, not files that import from *this* solution).

Flow

1. Compute total energy cost in O(n), derive energy training hours in O(1).

2. Walk the experience array left-to-right, tracking curexp. At each opponent, if curexp <= exp, bridge the gap with training hours. After each opponent, gain their experience.

3. Return the sum of energy and experience training hours.

Time complexity: O(n) — one pass for sum(energy), one pass for the experience simulation.

Space complexity: O(1) — only scalar accumulators.

Invariants

Error Handling

None. The function trusts its inputs match the LeetCode contract (equal-length lists, non-negative values). No validation, no exceptions.

Topics to Explore

Beliefs