From 1a6ed6bf1c503d5604f6e477f9ea80ebedee047e Mon Sep 17 00:00:00 2001 From: utsavkhemka21 <90024442+utsavkhemka21@users.noreply.github.com> Date: Sat, 22 Oct 2022 15:35:58 +0530 Subject: [PATCH] Add Leet Code Solution of 62 No Ques (#1061) Co-authored-by: Utsav Khemka --- leetcode/README.md | 185 ++++++++++++++++++++++----------------------- leetcode/src/62.c | 39 ++++++++++ 2 files changed, 131 insertions(+), 93 deletions(-) create mode 100644 leetcode/src/62.c diff --git a/leetcode/README.md b/leetcode/README.md index b2003739..30a5197a 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -1,96 +1,95 @@ -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| -|4|[Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [C](./src/4.c)|Hard| -|6|[ZigZag conversion](https://leetcode.com/problems/zigzag-conversion/) | [C](./src/4.c)|Medium| -|7|[Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [C](./src/7.c)|Easy| -|8|[String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi) | [C](./src/8.c)|Medium| -|9|[Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [C](./src/9.c)|Easy| -|11| [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [C](./src/11.c)|Medium| -|12|[Integer to Roman](https://leetcode.com/problems/integer-to-roman) | [C](./src/12.c)|Medium| -|13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [C](./src/13.c)|Easy| -|20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [C](./src/20.c)|Easy| -|21|[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [C](./src/21.c)|Easy| -|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [C](./src/24.c)|Medium| -|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [C](./src/26.c)|Easy| -|27|[Remove Element](https://leetcode.com/problems/remove-element/) | [C](./src/27.c)|Easy| -|28|[Implement strStr()](https://leetcode.com/problems/implement-strstr/) | [C](./src/28.c)|Easy| -|29|[Divide Two Integers](https://leetcode.com/problems/divide-two-integers/) | [C](./src/29.c)|Medium| -|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| -|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| -|94|[Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [C](./src/94.c)|Medium| -|101|[Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) | [C](./src/101.c)|Easy| -|104|[Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [C](./src/104.c)|Easy| -|108|[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | [C](./src/108.c)|Easy| -|109|[Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | [C](./src/109.c)|Medium| -|110|[Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [C](./src/110.c)|Easy| -|112|[Path Sum](https://leetcode.com/problems/path-sum/) | [C](./src/112.c)|Easy| -|118|[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) | [C](./src/118.c)|Easy| -|121|[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [C](./src/121.c)|Easy| -|125|[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [C](./src/125.c)|Easy| -|136|[Single Number](https://leetcode.com/problems/single-number/) | [C](./src/136.c)|Easy| -|141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) | [C](./src/141.c)|Easy| -|142|[Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | [C](./src/142.c)|Medium| -|153|[Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | [C](./src/153.c)|Medium| -|160|[Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [C](./src/160.c)|Easy| -|169|[Majority Element](https://leetcode.com/problems/majority-element/) | [C](./src/169.c)|Easy| -|173|[Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/) | [C](./src/173.c)|Medium| -|189|[Rotate Array](https://leetcode.com/problems/rotate-array) | [C](./src/189.c)|Easy| -|190|[Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [C](./src/190.c)|Easy| -|191|[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | [C](./src/191.c)|Easy| -|201|[Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [C](./src/201.c)|Medium| -|203|[Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) | [C](./src/203.c)|Easy| -|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [C](./src/206.c)|Easy| -|215|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [C](./src/215.c)|Medium| -|217|[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [C](./src/217.c)|Easy| -|226|[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | [C](./src/226.c)|Easy| -|231|[Power of Two](https://leetcode.com/problems/power-of-two/) | [C](./src/231.c)|Easy| -|234|[Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/) | [C](./src/234.c)|Easy| -|242|[Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [C](./src/242.c)|Easy| -|268|[Missing Number](https://leetcode.com/problems/missing-number/) | [C](./src/268.c)|Easy| -|278|[First Bad Version](https://leetcode.com/problems/first-bad-version/) | [C](./src/278.c)|Easy| -|283|[Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [C](./src/283.c)|Easy| -|287|[Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [C](./src/287.c)|Medium| -|344|[Reverse String](https://leetcode.com/problems/reverse-string/) | [C](./src/344.c)|Easy| -|367|[Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/) | [C](./src/367.c)|Easy| -|387|[First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [C](./src/387.c)|Easy| -|389|[Find the Difference](https://leetcode.com/problems/find-the-difference/) | [C](./src/389.c)|Easy| -|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [C](./src/404.c)|Easy| -|442|[Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | [C](./src/442.c)|Medium| -|461|[Hamming Distance](https://leetcode.com/problems/hamming-distance/) | [C](./src/461.c) |Easy| -|476|[Number Complement](https://leetcode.com/problems/number-complement/) | [C](./src/476.c)|Easy| -|509|[Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [C](./src/509.c)|Easy| -|520|[Detect Capital](https://leetcode.com/problems/detect-capital/) | [C](./src/520.c)|Easy| -|561|[Array Partition I](https://leetcode.com/problems/array-partition-i/) | [C](./src/561.c)|Easy| -|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [C](./src/617.c)|Easy| -|647|[Palindromic Substring](https://leetcode.com/problems/palindromic-substrings/) | [C](./src/647.c)|Medium| -|674|[Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | [C](./src/674.c)|Easy| -|700|[Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [C](./src/700.c)|Easy| -|701|[Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [C](./src/701.c)|Medium| -|704|[Binary Search](https://leetcode.com/problems/binary-search/) | [C](./src/704.c)|Easy| -|709|[To Lower Case](https://leetcode.com/problems/to-lower-case/) | [C](./src/709.c)|Easy| -|771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/) | [C](./src/771.c)|Easy| -|852|[Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/) | [C](./src/852.c)|Easy| -|876|[Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [C](./src/876.c)|Easy| -|905|[Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [C](./src/905.c)|Easy| -|917|[Reverse Only Letters](https://leetcode.com/problems/reverse-only-letters/) | [C](./src/917.c)|Easy| -|938|[Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/) | [C](./src/938.c)|Easy| -|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [C](./src/965.c)|Easy| -|977|[Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [C](./src/977.c)|Easy| -|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [C](./src/1089.c)|Easy| -|1184|[Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/) | [C](./src/1184.c)|Easy| -|1189|[Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [C](./src/1189.c)|Easy| -|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [C](./src/1207.c)|Easy| -|2130|[Maximum Twin Sum of a Linked List](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [C](./src/2130.c)|Medium| +| # | 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 | +| 4 | [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [C](./src/4.c) | Hard | +| 6 | [ZigZag conversion](https://leetcode.com/problems/zigzag-conversion/) | [C](./src/4.c) | Medium | +| 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [C](./src/7.c) | Easy | +| 8 | [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi) | [C](./src/8.c) | Medium | +| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [C](./src/9.c) | Easy | +| 11 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [C](./src/11.c) | Medium | +| 12 | [Integer to Roman](https://leetcode.com/problems/integer-to-roman) | [C](./src/12.c) | Medium | +| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [C](./src/13.c) | Easy | +| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [C](./src/20.c) | Easy | +| 21 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [C](./src/21.c) | Easy | +| 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [C](./src/24.c) | Medium | +| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [C](./src/26.c) | Easy | +| 27 | [Remove Element](https://leetcode.com/problems/remove-element/) | [C](./src/27.c) | Easy | +| 28 | [Implement strStr()](https://leetcode.com/problems/implement-strstr/) | [C](./src/28.c) | Easy | +| 29 | [Divide Two Integers](https://leetcode.com/problems/divide-two-integers/) | [C](./src/29.c) | Medium | +| 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 | +| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [C](./src/94.c) | Medium | +| 101 | [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) | [C](./src/101.c) | Easy | +| 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [C](./src/104.c) | Easy | +| 108 | [Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | [C](./src/108.c) | Easy | +| 109 | [Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | [C](./src/109.c) | Medium | +| 110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [C](./src/110.c) | Easy | +| 112 | [Path Sum](https://leetcode.com/problems/path-sum/) | [C](./src/112.c) | Easy | +| 118 | [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) | [C](./src/118.c) | Easy | +| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [C](./src/121.c) | Easy | +| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [C](./src/125.c) | Easy | +| 136 | [Single Number](https://leetcode.com/problems/single-number/) | [C](./src/136.c) | Easy | +| 141 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) | [C](./src/141.c) | Easy | +| 142 | [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | [C](./src/142.c) | Medium | +| 153 | [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | [C](./src/153.c) | Medium | +| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [C](./src/160.c) | Easy | +| 169 | [Majority Element](https://leetcode.com/problems/majority-element/) | [C](./src/169.c) | Easy | +| 173 | [Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/) | [C](./src/173.c) | Medium | +| 189 | [Rotate Array](https://leetcode.com/problems/rotate-array) | [C](./src/189.c) | Easy | +| 190 | [Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [C](./src/190.c) | Easy | +| 191 | [Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | [C](./src/191.c) | Easy | +| 201 | [Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [C](./src/201.c) | Medium | +| 203 | [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) | [C](./src/203.c) | Easy | +| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [C](./src/206.c) | Easy | +| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [C](./src/215.c) | Medium | +| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [C](./src/217.c) | Easy | +| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | [C](./src/226.c) | Easy | +| 231 | [Power of Two](https://leetcode.com/problems/power-of-two/) | [C](./src/231.c) | Easy | +| 234 | [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/) | [C](./src/234.c) | Easy | +| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [C](./src/242.c) | Easy | +| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [C](./src/268.c) | Easy | +| 278 | [First Bad Version](https://leetcode.com/problems/first-bad-version/) | [C](./src/278.c) | Easy | +| 283 | [Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [C](./src/283.c) | Easy | +| 287 | [Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [C](./src/287.c) | Medium | +| 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [C](./src/344.c) | Easy | +| 367 | [Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/) | [C](./src/367.c) | Easy | +| 387 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [C](./src/387.c) | Easy | +| 389 | [Find the Difference](https://leetcode.com/problems/find-the-difference/) | [C](./src/389.c) | Easy | +| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [C](./src/404.c) | Easy | +| 442 | [Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | [C](./src/442.c) | Medium | +| 461 | [Hamming Distance](https://leetcode.com/problems/hamming-distance/) | [C](./src/461.c) | Easy | +| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [C](./src/476.c) | Easy | +| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [C](./src/509.c) | Easy | +| 520 | [Detect Capital](https://leetcode.com/problems/detect-capital/) | [C](./src/520.c) | Easy | +| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i/) | [C](./src/561.c) | Easy | +| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [C](./src/617.c) | Easy | +| 647 | [Palindromic Substring](https://leetcode.com/problems/palindromic-substrings/) | [C](./src/647.c) | Medium | +| 674 | [Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | [C](./src/674.c) | Easy | +| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [C](./src/700.c) | Easy | +| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [C](./src/701.c) | Medium | +| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [C](./src/704.c) | Easy | +| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [C](./src/709.c) | Easy | +| 771 | [Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/) | [C](./src/771.c) | Easy | +| 852 | [Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/) | [C](./src/852.c) | Easy | +| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [C](./src/876.c) | Easy | +| 905 | [Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | [C](./src/905.c) | Easy | +| 917 | [Reverse Only Letters](https://leetcode.com/problems/reverse-only-letters/) | [C](./src/917.c) | Easy | +| 938 | [Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/) | [C](./src/938.c) | Easy | +| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [C](./src/965.c) | Easy | +| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [C](./src/977.c) | Easy | +| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [C](./src/1089.c) | Easy | +| 1184 | [Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/) | [C](./src/1184.c) | Easy | +| 1189 | [Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [C](./src/1189.c) | Easy | +| 1207 | [Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [C](./src/1207.c) | Easy | +| 2130 | [Maximum Twin Sum of a Linked List](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [C](./src/2130.c) | Medium | diff --git a/leetcode/src/62.c b/leetcode/src/62.c new file mode 100644 index 00000000..b2ac88d6 --- /dev/null +++ b/leetcode/src/62.c @@ -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]; +}