Skip to content

Commit

Permalink
style: use std::transform_reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
vil02 committed Sep 27, 2024
1 parent 5cc72eb commit 106f049
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions dynamic_programming/catalan_numbers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <cassert> /// for assert
#include <cstdint> /// for std::uint64_t
#include <cstdlib> /// for std::size_t
#include <numeric> /// for std::transform_reduce
#include <vector> /// for std::vector

/**
Expand All @@ -22,11 +23,9 @@ class catalan_numbers {
std::vector<value_type> known{1, 1};

value_type compute_next() {
value_type res = 0;
for (std::size_t i = 0; i < known.size(); ++i) {
res += known[i] * known[known.size() - i - 1];
}
return res;
return std::transform_reduce(known.begin(), known.end(), known.rbegin(),
static_cast<value_type>(), std::plus<>(),
std::multiplies<>());
}

void add() { known.push_back(this->compute_next()); }
Expand Down

0 comments on commit 106f049

Please sign in to comment.