problems: add unique paths
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
917aecb33f
commit
05c1800c4d
1 changed files with 27 additions and 0 deletions
27
problems/unique-paths.cpp
Normal file
27
problems/unique-paths.cpp
Normal file
|
@ -0,0 +1,27 @@
|
|||
class Solution {
|
||||
int right(const std::vector<int>& row, int size, int idx) const
|
||||
{
|
||||
if (idx >= size) {
|
||||
return 0;
|
||||
}
|
||||
return row[idx];
|
||||
}
|
||||
|
||||
public:
|
||||
int uniquePaths(int m, int n)
|
||||
{
|
||||
std::vector<int> bottom(n, 1);
|
||||
|
||||
for (int y = m - 2; y >= 0; y--) {
|
||||
std::vector<int> 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];
|
||||
}
|
||||
};
|
Loading…
Reference in a new issue