From c920f9bfd449acaf5a93fc8761d6e4f5b0b2d5ed Mon Sep 17 00:00:00 2001 From: Eatswap Date: Mon, 16 Oct 2023 22:28:54 +0800 Subject: [PATCH] add: 231015 --- cpp/2308/LC231015.cpp | 23 +++++++++++++++++++++++ cpp/2308/defs.h | 5 +++++ cpp/2308/main.cpp | 2 +- 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 cpp/2308/LC231015.cpp diff --git a/cpp/2308/LC231015.cpp b/cpp/2308/LC231015.cpp new file mode 100644 index 0000000..26bc023 --- /dev/null +++ b/cpp/2308/LC231015.cpp @@ -0,0 +1,23 @@ +#include "defs.h" +#include + +constexpr static int MOD = 1'000'000'007; + +int LC231015::numWays(int steps, int arrLen) { + if (arrLen == 1) + return 1; + int n = std::min(arrLen, 505); + int ans[2][512] {{1}, {}}; + for (int cs = 1; cs <= steps; ++cs) { + ans[cs & 1][0] = (ans[cs & 1 ^ 1][0] + ans[cs & 1 ^ 1][1]) % MOD; + for (int i = 1; i < n - 1; ++i) + ans[cs & 1][i] = ( + (ans[cs & 1 ^ 1][i - 1] + ans[(cs & 1) ^ 1][i]) % MOD + + ans[cs & 1 ^ 1][i + 1] + ) % MOD; + ans[cs & 1][n - 1] = (ans[cs & 1 ^ 1][n - 2] + ans[cs & 1 ^ 1][n - 1]) % MOD; + } + return ans[steps & 1][0]; +} + +using Solution = LC231015; diff --git a/cpp/2308/defs.h b/cpp/2308/defs.h index fa640a8..c62930f 100644 --- a/cpp/2308/defs.h +++ b/cpp/2308/defs.h @@ -36,4 +36,9 @@ public: static int paintWalls(const std::vector&, const std::vector&); }; +class LC231015 { +public: + static int numWays(int, int); +}; + #endif //LEETCODE_CPP_DEFS_H diff --git a/cpp/2308/main.cpp b/cpp/2308/main.cpp index 8be3ac7..b62171b 100644 --- a/cpp/2308/main.cpp +++ b/cpp/2308/main.cpp @@ -2,6 +2,6 @@ #include int main() { - std::cout << LC230927::decodeAtIndex("a2b3c4d5e6f7g8h9", 9); + std::cout << LC231015::numWays(4,2); return 0; } \ No newline at end of file