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]; + } +};