From 05c1800c4d1f823d73fd58ffc2caf7b0cdaddf0a Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Tue, 2 Aug 2022 11:40:31 +0200 Subject: [PATCH] problems: add unique paths Signed-off-by: Matej Focko --- problems/unique-paths.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 problems/unique-paths.cpp diff --git a/problems/unique-paths.cpp b/problems/unique-paths.cpp new file mode 100644 index 0000000..31c2358 --- /dev/null +++ b/problems/unique-paths.cpp @@ -0,0 +1,27 @@ +class Solution { + int right(const std::vector& row, int size, int idx) const + { + if (idx >= size) { + return 0; + } + return row[idx]; + } + +public: + int uniquePaths(int m, int n) + { + std::vector bottom(n, 1); + + for (int y = m - 2; y >= 0; y--) { + std::vector above(n, 0); + + for (int x = n - 1; x >= 0; x--) { + above[x] = right(above, n, x + 1) + bottom[x]; + } + + bottom = std::move(above); + } + + return bottom[0]; + } +};