generated from eyamenko/dotnet-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem14.cs
More file actions
41 lines (36 loc) · 1.23 KB
/
Problem14.cs
File metadata and controls
41 lines (36 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
namespace LeetCode;
/// <summary>
/// <see href="https://leetcode.com/problems/unique-paths/">Unique Paths</see>.
/// </summary>
#pragma warning disable CA1814
public static class Problem14
{
/// <summary>
/// A robot is located at the top-left corner of a m x n grid.
/// The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid.
/// Find how many possible unique paths are there.
/// Time complexity: O(m * n).
/// Space complexity: O(m * n).
/// </summary>
/// <param name="m">Number of rows.</param>
/// <param name="n">Number of columns.</param>
/// <returns>Number of unique paths.</returns>
public static int UniquePaths(int m, int n) => Traverse(new int[m, n], m - 1, n - 1, 0, 0);
private static int Traverse(int[,] grid, int m, int n, int i, int j)
{
if (i > m || j > n)
{
return 0;
}
if (grid[i, j] > 0)
{
return grid[i, j];
}
if (m == i && n == j)
{
return grid[i, j] = 1;
}
return grid[i, j] = Traverse(grid, m, n, i + 1, j) + Traverse(grid, m, n, i, j + 1);
}
}
#pragma warning restore CA1814