forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
25 lines (23 loc) · 875 Bytes
/
Solution.cs
File metadata and controls
25 lines (23 loc) · 875 Bytes
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
namespace LeetCodeNet.G0001_0100.S0063_unique_paths_ii {
// #Medium #Array #Dynamic_Programming #Matrix #Dynamic_Programming_I_Day_15
// #Top_Interview_150_Multidimensional_DP #2025_07_02_Time_0_ms_(100.00%)_Space_42.12_MB_(14.91%)
public class Solution {
public int UniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.Length;
int n = obstacleGrid[0].Length;
int[,] dp = new int[m, n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (obstacleGrid[i][j] == 1) {
dp[i, j] = 0;
} else if (i == 0 && j == 0) {
dp[i, j] = 1;
} else {
dp[i, j] = (i > 0 ? dp[i - 1, j] : 0) + (j > 0 ? dp[i, j - 1] : 0);
}
}
}
return dp[m - 1, n - 1];
}
}
}