From 3ed4e64b20f8339b8e3fcf23669ece74991becca Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 6 Jan 2024 22:51:12 +0100 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=E2=80=9C62.=20Unique=20Paths?= =?UTF-8?q?=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- cs/unique-paths.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 cs/unique-paths.cs diff --git a/cs/unique-paths.cs b/cs/unique-paths.cs new file mode 100644 index 0000000..bb702be --- /dev/null +++ b/cs/unique-paths.cs @@ -0,0 +1,25 @@ +public class Solution { + private static int Right(List row, int size, int idx) { + if (idx >= size) { + return 0; + } + + return row[idx]; + } + + public int UniquePaths(int m, int n) { + var bottom = new List(Enumerable.Repeat(1, n)); + + for (var y = m - 2; y >= 0; --y) { + var above = new List(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]; + } +}