diff --git a/README.md b/README.md index c501592..a3af1e8 100644 --- a/README.md +++ b/README.md @@ -252,12 +252,14 @@ Ace Coding Interview with 75 Qs [198]: ./src/page-2/198.%20House%20Robber/rob.ts [790]: ./src/page-8/790.%20Domino%20and%20Tromino%20Tiling/numTilings.ts -| DP - Multidimensional | | | -| --------------------------------------------------------- | -------- | ------ | -| 62. Unique Paths | Solution | Medium | -| 1143. Longest Common Subsequence | Solution | Medium | -| 714. Best Time to Buy and Sell Stock with Transaction Fee | Solution | Medium | -| 72. Edit Distance | Solution | Medium | +| DP - Multidimensional | | | +| --------------------------------------------------------- | -------------- | ------ | +| 62. Unique Paths | [Solution][62] | Medium | +| 1143. Longest Common Subsequence | Solution | Medium | +| 714. Best Time to Buy and Sell Stock with Transaction Fee | Solution | Medium | +| 72. Edit Distance | Solution | Medium | + +[62]: ./src/page-1/62.%20Unique%20Paths/uniquePaths.ts | Bit Manipulation | | | | --------------------------------------------- | ---------------- | ------ | @@ -647,7 +649,7 @@ Must-do List for Interview Prep | ----------------------------------- | --------------- | ------ | | 5. Longest Palindromic Substring | [Solution][5] | Medium | | 32. Longest Valid Parentheses | Solution | Hard | -| 62. Unique Paths | Solution | Medium | +| 62. Unique Paths | [Solution][62] | Medium | | 64. Minimum Path Sum | Solution | Medium | | 70. Climbing Stairs | [Solution][70] | Easy | | 72. Edit Distance | Solution | Medium | @@ -662,6 +664,7 @@ Must-do List for Interview Prep | 1143. Longest Common Subsequence | Solution | Medium | [5]: ./src/page-1/5.%20Longest%20Palindromic%20Substring/longestPalindrome.ts +[62]: ./src/page-1/62.%20Unique%20Paths/uniquePaths.ts [70]: ./src/page-1/70.%20Climbing%20Stairs/climbStairs.ts [118]: ./src/page-2/118.%20Pascal's%20Triangle/generate.ts [198]: ./src/page-2/198.%20House%20Robber/rob.ts diff --git a/src/page-1/62. Unique Paths/uniquePaths.test.ts b/src/page-1/62. Unique Paths/uniquePaths.test.ts new file mode 100644 index 0000000..0153a82 --- /dev/null +++ b/src/page-1/62. Unique Paths/uniquePaths.test.ts @@ -0,0 +1,8 @@ +import { uniquePaths } from './uniquePaths'; + +describe('62. Unique Paths', () => { + test('uniquePaths', () => { + expect(uniquePaths(3, 7)).toBe(28); + expect(uniquePaths(3, 2)).toBe(3); + }); +}); diff --git a/src/page-1/62. Unique Paths/uniquePaths.ts b/src/page-1/62. Unique Paths/uniquePaths.ts new file mode 100644 index 0000000..aed1e42 --- /dev/null +++ b/src/page-1/62. Unique Paths/uniquePaths.ts @@ -0,0 +1,22 @@ +type UniquePaths = (m: number, n: number) => number; + +/** + * Accepted + */ +export const uniquePaths: UniquePaths = (m, n) => { + const dp: number[][] = Array.from({ length: m }, () => Array(n).fill(0)); + + // Initialize the first row and first column + for (let i = 0; i < m; i++) dp[i][0] = 1; + for (let j = 0; j < n; j++) dp[0][j] = 1; + + // Fill in the rest of the dp array + for (let i = 1; i < m; i++) { + for (let j = 1; j < n; j++) { + dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; + } + } + + // The bottom-right corner will contain the number of unique paths + return dp[m - 1][n - 1]; +};