feat: add LeetCode problem 62 (#1062)

Co-authored-by: Utsav Khemka <utsav9125368000@gmail.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
utsavkhemka21 2022-10-29 07:00:13 +05:30 committed by GitHub
parent 0cd4f6b691
commit 27145d7ce4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

View File

@ -26,6 +26,7 @@
| 38 | [Count and Say](https://leetcode.com/problems/count-and-say/) | [C](./src/38.c) | Easy |
| 53 | [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | [C](./src/53.c) | Easy |
| 62 | [Unique Paths](https://leetcode.com/problems/unique-paths/description/) | Medium |
| 63 | [Unique Paths II](https://leetcode.com/problems/unique-paths-ii/) | [C](./src/63.c) | Medium |
| 66 | [Plus One](https://leetcode.com/problems/plus-one/) | [C](./src/66.c) | Easy |
| 82 | [Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/) | [C](./src/82.c) | Medium |
| 83 | [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) | [C](./src/83.c) | Easy |

39
leetcode/src/63.c Normal file
View File

@ -0,0 +1,39 @@
/*
I walk through the grids and record the path numbers at the
same time.
By using a 2D array called paths, it will add up possible so
urce path and save the number.
Noted that:
if the destination has obstacle, we can't reach it
the first grid (paths[0][0]) always set as 1 our previous
path source is either from top or left border of grid has
different condition
*/
int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridSize,
int* obstacleGridColSize)
{
if (obstacleGrid[obstacleGridSize - 1][*obstacleGridColSize - 1] == 1)
{
return 0;
}
int paths[obstacleGridSize][*obstacleGridColSize];
for (int i = 0; i < obstacleGridSize; i++)
{
for (int j = 0; j < *obstacleGridColSize; j++)
{
if (obstacleGrid[i][j])
{
paths[i][j] = 0;
}
else
{
paths[i][j] = (i == 0 && j == 0)
? 1
: ((i == 0 ? 0 : paths[i - 1][j]) +
(j == 0 ? 0 : paths[i][j - 1]));
}
}
}
return paths[obstacleGridSize - 1][*obstacleGridColSize - 1];
}