Skip to content

Commit 02e620d

Browse files
authored
Merge pull request #2247 from geegong/main
[geegong] WEEK 08 solutions
2 parents 2253201 + 0e73afc commit 02e620d

File tree

1 file changed

+77
-22
lines changed

1 file changed

+77
-22
lines changed

โ€Žlongest-common-subsequence/Geegong.javaโ€Ž

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,91 @@ public class Geegong {
2020
* @return
2121
*/
2222
public int longestCommonSubsequence(String text1, String text2) {
23-
char[] chText1 = text1.toCharArray();
24-
char[] chText2 = text2.toCharArray();
25-
26-
// text1๋งŒ ๊ฐ ์›์†Œ ๋ณ„๋กœ char๊ฐ€ ๊ฐ€์ง€๋Š” index๋ฅผ array ๋กœ ๊ฐ–๋Š” ๋ฐฐ์—ด์„ ์ƒ์„ฑ
27-
List<Integer>[] positionIndices = new List[26];
28-
for (int index = 0; index<chText1.length; index++) {
29-
if (positionIndices[chText1[index] - 'a'] == null) {
30-
positionIndices[chText1[index] - 'a'] = new ArrayList();
31-
positionIndices[chText1[index] - 'a'].add(index);
32-
} else {
33-
positionIndices[chText1[index] - 'a'].add(index);
34-
}
23+
24+
/**
25+
* DP ๋ฅผ ์ด์šฉํ•œ LIS ๊ตฌํ•˜๊ธฐ
26+
* 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด text1, text2 ๋“ค์„ ํ•˜๋‚˜์”ฉ ํ›‘์–ด๊ฐ€๋ฉฐ
27+
* ์ด์ „์— ์ผ์น˜ํ–ˆ๋˜ ๊ฐ character ๋ณ„๋กœ ํšŸ์ˆ˜๋“ค์„ ๊ธฐ๋กํ•ด๊ฐ€๋ฉฐ ๋” LIS ๋กœ ๋ถ™์ผ ์ˆ˜ ์žˆ๋Š”์ง€ ์ •๋ฆฌํ•ด๊ฐ€๋Š” ๊ธฐ๋ฒ•
28+
* ์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ : https://velog.io/@emplam27/%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-%EA%B7%B8%EB%A6%BC%EC%9C%BC%EB%A1%9C-%EC%95%8C%EC%95%84%EB%B3%B4%EB%8A%94-LCS-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-Longest-Common-Substring%EC%99%80-Longest-Common-Subsequence
29+
* time complexity : O(M * N) => O (N)
30+
* space complexity : O(M * N) => O (N)
31+
*/
32+
char[] ch1 = text1.toCharArray();
33+
char[] ch2 = text2.toCharArray();
34+
35+
int[][] LISArr = new int[ch1.length + 1][ch2.length + 1];
36+
37+
// first, fill zeros in first row, first column for convenient
38+
for (int idx=0; idx<=ch2.length; idx++) {
39+
LISArr[0][idx] = 0;
3540
}
3641

42+
for (int idx=0; idx<=ch1.length; idx++) {
43+
LISArr[idx][0] = 0;
44+
}
3745

38-
// ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ LIS ๋ฅผ ๊ตฌํ• ๊ฒƒ์ž„
39-
List<Integer> indices = new ArrayList<>();
40-
for (int index=0; index<chText2.length; index++) {
46+
int maxLIS = 0;
47+
for (int index1 = 0; index1<ch1.length; index1++) {
48+
for (int index2 = 0; index2<ch2.length; index2++) {
4149

42-
char find = chText2[index];
43-
if (positionIndices[find-'a'] != null && positionIndices[find-'a'].size() > 0) {
44-
// ์—ญ์ˆœ (LIS ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ผ๋ถ€๋Ÿฌ ๋’ค์ง‘์–ด ๋†’์Œ, ์ฆ‰ ๊ฐ char ๋ณ„ LIS ๋ฅผ ๊ตฌํ• ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—..)
45-
// positionIndices ์—์„œ ๊ตฌํ–ˆ๋˜ ๊ฐ’๋“ค์„ ๊ทธ๋Œ€๋กœ addAll ํ•œ๋‹ค๋ฉด ์˜ค๋ฆ„์ฐจ์ˆœ์ด ๋˜๊ธฐ๋•Œ๋ฌธ์— ์ •ํ™•ํ•œ LIS ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์—†๋‹ค
50+
// if same, then
51+
if (ch1[index1] == ch2[index2]) {
52+
// LISArr ์˜ ์ธ๋ฑ์Šค๊ฐ’์ด +1 ์ด ๋˜๋Š” ์ด์œ ๋Š” LISArr ์˜ ํฌ๊ธฐ๊ฐ€ text1, text2 ํฌ๊ธฐ + 1 ๋งŒํผ ๋งŒ๋“ค์—ˆ๊ธฐ ๋•Œ๋ฌธ
53+
LISArr[index1 + 1][index2 + 1] = LISArr[index1][index2] + 1;
54+
} else {
55+
int biggerOne = Math.max(LISArr[index1][index2 + 1], LISArr[index1 + 1][index2]);
56+
LISArr[index1 + 1][index2 + 1] = biggerOne;
57+
}
4658

47-
indices.addAll(positionIndices[find-'a'].stream().sorted(Comparator.reverseOrder()).toList());
59+
maxLIS = Math.max(maxLIS, LISArr[index1 + 1][index2 + 1]);
4860
}
4961
}
5062

51-
// find LIS
52-
return findLIS(indices).size();
63+
return maxLIS;
64+
65+
66+
67+
/**
68+
* LIS, LCS ์™€ ์—ฐ๊ด€์ง€์–ด์„œ ํ’€์ด
69+
* 1. ๋จผ์ € text1์„ ํ›‘์–ด๋ณด๋Š”๋ฐ text1์— ์ค‘๋ณต๋˜๋Š” ์•ŒํŒŒ๋ฒณ์ด ์žˆ์„ ์ˆ˜ ์žˆ๊ธฐ์— ๊ฐ ์บ๋ฆญํ„ฐ๋ณ„๋กœ ์ธ๋ฑ์Šค๋“ค์„ ์ €์žฅ (๊ฐ ์›์†Œ๊ฐ€ arrayList๊ฐ€ ์žˆ๋Š” ๋ฐฐ์—ด)
70+
* 2. text2 ๋ฅผ ํ›‘์œผ๋ฉด์„œ ๋งค์นญ๋˜๋Š” ์บ๋ฆญํ„ฐ๋“ค์— ๋Œ€ํ•ด์„œ๋งŒ 1์—์„œ ์ €์žฅ๋œ ์ธ๋ฑ์Šค๋“ค์„ ํ•œ ๋ฐฐ์—ด์•ˆ์— ๋‚˜์—ด
71+
* -> ์ด๋–„ ๋‚˜์—ดํ•ด์„œ ๋„ฃ์„๋•Œ๋งˆ๋‹ค ์—ญ์ˆœ์œผ๋กœ ์ง‘์–ด๋„ฃ๋Š”๊ฒŒ ์ค‘์š”! (์™œ๋ƒ๋ฉด ์ด ๋‚˜์—ด๋œ ์ธ๋ฑ์Šค๋“ค์„ ๊ฐ€์ง€๊ณ  LIS๋ฅผ ๊ตฌํ• ๊ฑฐ๋ผ์„œ)
72+
* 3. ๋‚˜์—ด๋œ ์ธ๋ฑ์Šค๋“ค์˜ ๊ฐ’๋“ค์„ ๊ฐ€์ง€๊ณ  LIS ๋ฅผ ๊ตฌํ•œ๋‹ค, ์ฆ‰ ์ด LIS์˜ ๊ธธ์ด๊ฐ€ LCS ์˜ ๊ธธ์ด๊ฐ€ ๋œ๋‹ค..!! (์™€์šฐ ์‹ ๊ธฐ)
73+
* ๊ทธ๋Ÿฌ๋‚˜ leet code ์— ๋Œ๋ ธ์„ ๋•Œ runtime ์ด ์˜ ์ข‹์ง€๋Š” ์•Š์Œ
74+
*
75+
* time complexity : O(M + N logN) => text2์— ๋Œ€ํ•ด์„œ ๋ฌธ์ž๋งˆ๋‹ค ๋ฐ”์ด๋„ˆ๋ฆฌ์„œ์น˜๊ฐ€ ์ˆ˜ํ–‰๋จ
76+
* space complexity : O(M+N)
77+
*/
78+
// char[] chText1 = text1.toCharArray();
79+
// char[] chText2 = text2.toCharArray();
80+
//
81+
// // text1๋งŒ ๊ฐ ์›์†Œ ๋ณ„๋กœ char๊ฐ€ ๊ฐ€์ง€๋Š” index๋ฅผ array ๋กœ ๊ฐ–๋Š” ๋ฐฐ์—ด์„ ์ƒ์„ฑ
82+
// List<Integer>[] positionIndices = new List[26];
83+
// for (int index = 0; index<chText1.length; index++) {
84+
// if (positionIndices[chText1[index] - 'a'] == null) {
85+
// positionIndices[chText1[index] - 'a'] = new ArrayList();
86+
// positionIndices[chText1[index] - 'a'].add(index);
87+
// } else {
88+
// positionIndices[chText1[index] - 'a'].add(index);
89+
// }
90+
// }
91+
//
92+
//
93+
// // ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ LIS ๋ฅผ ๊ตฌํ• ๊ฒƒ์ž„
94+
// List<Integer> indices = new ArrayList<>();
95+
// for (int index=0; index<chText2.length; index++) {
96+
//
97+
// char find = chText2[index];
98+
// if (positionIndices[find-'a'] != null && positionIndices[find-'a'].size() > 0) {
99+
// // ์—ญ์ˆœ (LIS ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ผ๋ถ€๋Ÿฌ ๋’ค์ง‘์–ด ๋†’์Œ, ์ฆ‰ ๊ฐ char ๋ณ„ LIS ๋ฅผ ๊ตฌํ• ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—..)
100+
// // positionIndices ์—์„œ ๊ตฌํ–ˆ๋˜ ๊ฐ’๋“ค์„ ๊ทธ๋Œ€๋กœ addAll ํ•œ๋‹ค๋ฉด ์˜ค๋ฆ„์ฐจ์ˆœ์ด ๋˜๊ธฐ๋•Œ๋ฌธ์— ์ •ํ™•ํ•œ LIS ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์—†๋‹ค
101+
//
102+
// indices.addAll(positionIndices[find-'a'].stream().sorted(Comparator.reverseOrder()).toList());
103+
// }
104+
// }
105+
//
106+
// // find LIS
107+
// return findLIS(indices).size();
53108
}
54109

55110
public List<Integer> findLIS(List<Integer> source) {

0 commit comments

Comments
ย (0)