From 27145d7ce4a87dc4585a056acaec185c634b2033 Mon Sep 17 00:00:00 2001 From: utsavkhemka21 <90024442+utsavkhemka21@users.noreply.github.com> Date: Sat, 29 Oct 2022 07:00:13 +0530 Subject: [PATCH] feat: add LeetCode problem 62 (#1062) Co-authored-by: Utsav Khemka Co-authored-by: David Leal --- leetcode/README.md | 1 + leetcode/src/63.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 leetcode/src/63.c diff --git a/leetcode/README.md b/leetcode/README.md index 8e9fe6a6..c1223384 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -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 | diff --git a/leetcode/src/63.c b/leetcode/src/63.c new file mode 100644 index 00000000..d26106fd --- /dev/null +++ b/leetcode/src/63.c @@ -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]; +}