Add Leet Code Solution of 62 No Ques (#1061)

Co-authored-by: Utsav Khemka <utsav9125368000@gmail.com>
This commit is contained in:
utsavkhemka21 2022-10-22 15:35:58 +05:30 committed by GitHub
parent 9502fd54ef
commit 1a6ed6bf1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 131 additions and 93 deletions

View File

@ -1,11 +1,9 @@
LeetCode
========
# LeetCode
### LeetCode Algorithm
| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
| ---- | ------------------------------------------------------------------------------------------------------------------------------- | ----------------- | ---------- |
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [C](./src/1.c) | Easy |
| 2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [C](./src/2.c) | Medium |
| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [C](./src/3.c) | Medium |
@ -27,6 +25,7 @@ LeetCode
| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [C](./src/35.c) | Easy |
| 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 |
| 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/62.c Normal file
View File

@ -0,0 +1,39 @@
// Dynamic programming can be applied here, because every solved sub-problem has
// an optimal sub-solution
// Searching backwards from end to start, we can incrementally calculate number
// of paths to destination. i.e. start from bottom-right, and calculate
// leftwards (lowest row should all be 1). then go to second-last-row, rightmost
// column, and calculate leftwards the last cell to be calculated is the start
// location (0, 0). The iteration ordering is intentional: the inner loop
// iterates the contents of each vector, the outer loop iterates each vector.
// This is more cache-friendly.
// Example below, calculated from right-to-left, bottom-to-top.
// 7 by 3 grid
// 28 21 15 10 6 3 1
// 7 6 5 4 3 2 1
// 1 1 1 1 1 1 1
int uniquePaths(int m, int n)
{
int dp[m][n];
for (int column = 0; column < n; column++)
{
dp[0][column] = 1;
}
for (int row = 1; row < m; row++)
{
dp[row][0] = 1;
}
for (int row = 1; row < m; row++)
{
for (int column = 1; column < n; column++)
{
dp[row][column] = dp[row - 1][column] + dp[row][column - 1];
}
}
return dp[m - 1][n - 1];
}