Skip to content

Commit

Permalink
249th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Nov 3, 2024
1 parent ff750c6 commit 9f094db
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 23 deletions.
48 changes: 25 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,15 @@ 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][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 |
| DP - Multidimensional | | |
| --------------------------------------------------------- | ---------------- | ------ |
| 62. Unique Paths | [Solution][62] | Medium |
| 1143. Longest Common Subsequence | [Solution][1143] | 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
[1143]: ./src/page-11/1143.%20Longest%20Common%20Subsequence/longestCommonSubsequence.ts

| Bit Manipulation | | |
| --------------------------------------------- | ---------------- | ------ |
Expand Down Expand Up @@ -645,29 +646,30 @@ Must-do List for Interview Prep
[236]: ./src/page-3/236.%20Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/lowestCommonAncestor.ts
[437]: ./src/page-5/437.%20Path%20Sum%20III/pathSum.ts

| Dynamic Programming | | |
| ----------------------------------- | --------------- | ------ |
| 5. Longest Palindromic Substring | [Solution][5] | Medium |
| 32. Longest Valid Parentheses | Solution | Hard |
| 62. Unique Paths | [Solution][62] | Medium |
| 64. Minimum Path Sum | Solution | Medium |
| 70. Climbing Stairs | [Solution][70] | Easy |
| 72. Edit Distance | Solution | Medium |
| 118. Pascal's Triangle | [Solution][118] | Easy |
| 139. Word Break | Solution | Medium |
| 152. Maximum Product Subarray | Solution | Medium |
| 198. House Robber | [Solution][198] | Medium |
| 279. Perfect Squares | Solution | Medium |
| 300. Longest Increasing Subsequence | Solution | Medium |
| 322. Coin Change | Solution | Medium |
| 416. Partition Equal Subset Sum | Solution | Medium |
| 1143. Longest Common Subsequence | Solution | Medium |
| Dynamic Programming | | |
| ----------------------------------- | ---------------- | ------ |
| 5. Longest Palindromic Substring | [Solution][5] | Medium |
| 32. Longest Valid Parentheses | Solution | Hard |
| 62. Unique Paths | [Solution][62] | Medium |
| 64. Minimum Path Sum | Solution | Medium |
| 70. Climbing Stairs | [Solution][70] | Easy |
| 72. Edit Distance | Solution | Medium |
| 118. Pascal's Triangle | [Solution][118] | Easy |
| 139. Word Break | Solution | Medium |
| 152. Maximum Product Subarray | Solution | Medium |
| 198. House Robber | [Solution][198] | Medium |
| 279. Perfect Squares | Solution | Medium |
| 300. Longest Increasing Subsequence | Solution | Medium |
| 322. Coin Change | Solution | Medium |
| 416. Partition Equal Subset Sum | Solution | Medium |
| 1143. Longest Common Subsequence | [Solution][1143] | 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
[1143]: ./src/page-11/1143.%20Longest%20Common%20Subsequence/longestCommonSubsequence.ts

| Graph | | |
| ---------------------- | --------------- | ------ |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { longestCommonSubsequence } from './longestCommonSubsequence';

describe('1143. Longest Common Subsequence', () => {
test('longestCommonSubsequence', () => {
expect(longestCommonSubsequence('abcde', 'ace')).toBe(3);
expect(longestCommonSubsequence('abc', 'abc')).toBe(3);
expect(longestCommonSubsequence('abc', 'def')).toBe(0);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
type LongestCommonSubsequence = (text1: string, text2: string) => number;

/**
* Accepted
*/
export const longestCommonSubsequence: LongestCommonSubsequence = (text1, text2) => {
const m = text1.length;
const n = text2.length;

// Initialize a (m+1) x (n+1) DP array with zeros
const dp: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));

// Fill in the DP array
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (text1[i - 1] === text2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}

// The length of the longest common subsequence is found at dp[m][n]
return dp[m][n];
};

0 comments on commit 9f094db

Please sign in to comment.