mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
cs: add “62. Unique Paths”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
64008089ff
commit
3ed4e64b20
1 changed files with 25 additions and 0 deletions
25
cs/unique-paths.cs
Normal file
25
cs/unique-paths.cs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
public class Solution {
|
||||||
|
private static int Right(List<int> row, int size, int idx) {
|
||||||
|
if (idx >= size) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return row[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
public int UniquePaths(int m, int n) {
|
||||||
|
var bottom = new List<int>(Enumerable.Repeat(1, n));
|
||||||
|
|
||||||
|
for (var y = m - 2; y >= 0; --y) {
|
||||||
|
var above = new List<int>(Enumerable.Repeat(0, n));
|
||||||
|
|
||||||
|
for (var x = n - 1; x >= 0; --x) {
|
||||||
|
above[x] = Right(above, n, x + 1) + bottom[x];
|
||||||
|
}
|
||||||
|
|
||||||
|
bottom = above;
|
||||||
|
}
|
||||||
|
|
||||||
|
return bottom[0];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue