Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2b11869

Browse files
add 1143
1 parent2b3c88e commit2b11869

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ _If you like this project, please leave me a star._ ★
344344
|1152|[Analyze User Website Visit Pattern](https://leetcode.com/problems/analyze-user-website-visit-pattern/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1152.java)|[:tv:](https://youtu.be/V510Lbtrm5s)|Medium|HashTable, Sort, Array|
345345
|1150|[Check If a Number Is Majority Element in a Sorted Array](https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1150.java)|[:tv:](https://youtu.be/-t2cdVs9cKk)|Easy||
346346
|1146|[Snapshot Array](https://leetcode.com/problems/snapshot-array/)|[Javascript](./javascript/_1146.js)|| Easy||
347+
|1143|[Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1143.java) | |Medium||String, DP
347348
|1138|[Alphabet Board Path](https://leetcode.com/problems/alphabet-board-path/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1138.java)|[:tv:](https://youtu.be/rk-aB4rEOyU)| Medium|HashTable, String|
348349
|1137|[N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1137.java)||Easy||
349350
|1136|[Parallel Courses](https://leetcode.com/problems/parallel-courses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1136.java)||Medium||
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
packagecom.fishercoder.solutions;
2+
3+
publicclass_1143 {
4+
publicstaticclassSolution1 {
5+
/**
6+
* credit: https://leetcode.com/problems/longest-common-subsequence/solution/
7+
* <p>
8+
* Recall that there are two different techniques we can use to implement a dynamic programming solution; memoization and tabulation.
9+
* <p>
10+
* Memoization is where we add caching to a function (that has no side effects). In dynamic programming, it is typically used on recursive functions for a top-down solution that starts with the initial problem and then recursively calls itself to solve smaller problems.
11+
* Tabulation uses a table to keep track of subproblem results and works in a bottom-up manner: solving the smallest subproblems before the large ones, in an iterative manner. Often, people use the words "tabulation" and "dynamic programming" interchangeably.
12+
* <p>
13+
* For most people, it's easiest to start by coming up with a recursive brute-force solution and then adding memoization to it. After that, they then figure out how to convert it into an (often more desired) bottom-up tabulated algorithm.
14+
*/
15+
publicintlongestCommonSubsequence(Stringtext1,Stringtext2) {
16+
if (text1 ==null ||text1.length() ==0 ||text2 ==null ||text2.length() ==0) {
17+
return0;
18+
}
19+
intm =text1.length();
20+
intn =text2.length();
21+
int[][]dp =newint[m +1][n +1];
22+
for (inti =0;i <m;i++) {
23+
for (intj =0;j <n;j++) {
24+
dp[i][j] = -1;
25+
}
26+
}
27+
returntopDownRecursiveSolve(dp,0,0,text1,text2);
28+
}
29+
30+
privateinttopDownRecursiveSolve(int[][]dp,inti,intj,Stringtext1,Stringtext2) {
31+
if (dp[i][j] != -1) {
32+
returndp[i][j];
33+
}
34+
//option1: we don't include text1.charAt(i) in the optimal solution
35+
intoption1 =topDownRecursiveSolve(dp,i +1,j,text1,text2);
36+
//option2: we do include text1.charAt(i) in the optimal solution as long as a match in text2 at or after j does exist
37+
intfirstOccurence =text2.indexOf(text1.charAt(i),j);
38+
intoption2 =0;
39+
if (firstOccurence != -1) {
40+
option2 =1 +topDownRecursiveSolve(dp,i +1,firstOccurence +1,text1,text2);
41+
}
42+
dp[i][j] =Math.max(option1,option2);
43+
returndp[i][j];
44+
}
45+
}
46+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1143;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1143Test {
10+
privatestatic_1143.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1143.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(3,solution1.longestCommonSubsequence("abcde","ace"));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(3,solution1.longestCommonSubsequence("abc","abc"));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals(0,solution1.longestCommonSubsequence("abc","def"));
30+
}
31+
32+
@Test
33+
publicvoidtest4() {
34+
assertEquals(2,solution1.longestCommonSubsequence("ezupkr","ubmrapg"));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp