-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minimum Hours of Training to Win a Competition
- Loading branch information
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
solutions/minimum-hours-of-training-to-win-a-competition/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_catch(test_minimum_hours_of_training_to_win_a_competition test.cpp) |
30 changes: 30 additions & 0 deletions
30
solutions/minimum-hours-of-training-to-win-a-competition/solution.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include <numeric> | ||
#include <vector> | ||
|
||
class Solution { | ||
public: | ||
static int minNumberOfHours(int initialEnergy, int initialExperience, | ||
const std::vector<int> &energy, | ||
const std::vector<int> &experience) { | ||
return trainEnergy(initialEnergy, energy) + | ||
trainExperience(initialExperience, experience); | ||
} | ||
|
||
private: | ||
static int trainEnergy(int initialEnergy, const std::vector<int> &energy) { | ||
const auto sum = std::accumulate(energy.begin(), energy.end(), 0); | ||
return std::max(0, sum - initialEnergy + 1); | ||
} | ||
|
||
static int trainExperience(int initialExperience, | ||
const std::vector<int> &experience) { | ||
int max = 0, prev = 0; | ||
for (auto e : experience) { | ||
max = std::max(max, e - prev); | ||
prev += e; | ||
} | ||
return std::max(0, max - initialExperience + 1); | ||
} | ||
}; |
37 changes: 37 additions & 0 deletions
37
solutions/minimum-hours-of-training-to-win-a-competition/test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
struct TestCase { | ||
int initialEnergy; | ||
int initialExperience; | ||
std::vector<int> energy; | ||
std::vector<int> experience; | ||
int expected; | ||
}; | ||
|
||
std::vector<TestCase> test_cases{ | ||
{ | ||
.initialEnergy = 5, | ||
.initialExperience = 3, | ||
.energy{1, 4, 3, 2}, | ||
.experience{2, 6, 3, 1}, | ||
.expected = 8, | ||
}, | ||
{ | ||
.initialEnergy = 2, | ||
.initialExperience = 4, | ||
.energy{1}, | ||
.experience{3}, | ||
.expected = 0, | ||
}, | ||
}; | ||
|
||
for (const auto &[initialEnergy, initialExperience, energy, experience, | ||
expected] : test_cases) { | ||
const auto actual = Solution::minNumberOfHours( | ||
initialEnergy, initialExperience, energy, experience); | ||
REQUIRE(expected == actual); | ||
} | ||
} |