classSolution: defminNumberOfHours(self, initialEnergy: int, initialExperience: int, energy: List[int], experience: List[int]) -> int: sm = sum(energy) trainingHours = 0if initialEnergy > sm else sm + 1 - initialEnergy for e in experience: if initialExperience <= e: trainingHours += 1 + (e - initialExperience) initialExperience = 2 * e + 1 else: initialExperience += e return trainingHours
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { publicintminNumberOfHours(int initialEnergy, int initialExperience, int[] energy, int[] experience) { intsum=0; for (int e : energy) { sum += e; } inttrainingHours= initialEnergy > sum ? 0 : sum + 1 - initialEnergy; for (int e : experience) { if (initialExperience <= e) { trainingHours += 1 + (e - initialExperience); initialExperience = 2 * e + 1; } else { initialExperience += e; } } return trainingHours; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicclassSolution { publicintMinNumberOfHours(int initialEnergy, int initialExperience, int[] energy, int[] experience) { int sum = 0; foreach (int e in energy) { sum += e; } int trainingHours = initialEnergy > sum ? 0 : sum + 1 - initialEnergy; foreach (int e in experience) { if (initialExperience <= e) { trainingHours += 1 + (e - initialExperience); initialExperience = 2 * e + 1; } else { initialExperience += e; } } return trainingHours; } }
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public: intminNumberOfHours(int initialEnergy, int initialExperience, vector<int>& energy, vector<int>& experience){ int sum = 0; for (int e : energy) { sum += e; } int trainingHours = initialEnergy > sum ? 0 : sum + 1 - initialEnergy; for (int e : experience) { if (initialExperience <= e) { trainingHours += 1 + (e - initialExperience); initialExperience = 2 * e + 1; } else { initialExperience += e; } } return trainingHours; } };
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
intminNumberOfHours(int initialEnergy, int initialExperience, int* energy, int energySize, int* experience, int experienceSize) { int sum = 0; for (int i = 0; i < energySize; i++) { sum += energy[i]; } int trainingHours = initialEnergy > sum ? 0 : sum + 1 - initialEnergy; for (int i = 0; i < experienceSize; i++) { int e = experience[i]; if (initialExperience <= e) { trainingHours += 1 + (e - initialExperience); initialExperience = 2 * e + 1; } else { initialExperience += e; } } return trainingHours; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var minNumberOfHours = function(initialEnergy, initialExperience, energy, experience) { let sum = 0; for (const e of energy) { sum += e; } let trainingHours = initialEnergy > sum ? 0 : sum + 1 - initialEnergy; for (const e of experience) { if (initialExperience <= e) { trainingHours += 1 + (e - initialExperience); initialExperience = 2 * e + 1; } else { initialExperience += e; } } return trainingHours; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
funcminNumberOfHours(initialEnergy int, initialExperience int, energy []int, experience []int)int { sum := 0 for _, e := range energy { sum += e } trainingHours := 0 if initialEnergy <= sum { trainingHours = sum + 1 - initialEnergy } for _, e := range experience { if initialExperience <= e { trainingHours += 1 + (e - initialExperience) initialExperience = 2*e + 1 } else { initialExperience += e } } return trainingHours }