Skip to content

Commit 0e821ae

Browse files
committed
feat(Codewars): WIP onlinePlatform/codewars/4kyu/counting_change_combinations.cc
1 parent 5a61103 commit 0e821ae

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/onlinePlatform/codewars/4kyu/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,13 @@ target_link_libraries(
99
GTest::gtest_main
1010
)
1111
gtest_discover_tests(matrix_determinant_google_test)
12+
13+
add_executable(
14+
counting_change_combinations_google_test
15+
counting_change_combinations_google_test.cc
16+
)
17+
target_link_libraries(
18+
counting_change_combinations_google_test
19+
GTest::gtest_main
20+
)
21+
gtest_discover_tests(counting_change_combinations_google_test)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// https://www.codewars.com/kata/5263a84ffcadb968b6000513/train/cpp
2+
3+
#include <vector>
4+
#include <unordered_set>
5+
6+
using namespace std;
7+
8+
unsigned long long countChange(const unsigned int money, const std::vector<unsigned int>& coins) {
9+
// your code here
10+
11+
// TODO: is there duplicates in coins?
12+
13+
std::unordered_set<unsigned int> uniqueCoins(begin(coins), end(coins));
14+
15+
std::vector<long long> counts(money + 1);
16+
counts[0] = 1;
17+
for (auto coin: uniqueCoins) {
18+
std::vector<long long> prev(begin(counts), end(counts));
19+
counts = std::vector<long long>(money + 1);
20+
21+
for (auto i = money; i >= coin; --i) {
22+
counts[i] += prev[i - coin];
23+
}
24+
}
25+
26+
return counts[money];
27+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <gtest/gtest.h>
2+
#include <string>
3+
#include <vector>
4+
5+
#include "counting_change_combinations.cc"
6+
7+
TEST(CountingChangeCombinations, countChange_test) {
8+
EXPECT_EQ(countChange(4, {1,2}), 3);
9+
10+
EXPECT_EQ(countChange(10, {5,2,3}), 4);
11+
}

0 commit comments

Comments
 (0)