From eb1a6438c2c322caf63bb8607a8be79e6f6515c6 Mon Sep 17 00:00:00 2001 From: jinvicky Date: Sun, 7 Sep 2025 11:25:23 +0900 Subject: [PATCH 1/3] longest common subsequence solution --- longest-common-subsequence/jinvicky.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 longest-common-subsequence/jinvicky.java diff --git a/longest-common-subsequence/jinvicky.java b/longest-common-subsequence/jinvicky.java new file mode 100644 index 000000000..eb8876388 --- /dev/null +++ b/longest-common-subsequence/jinvicky.java @@ -0,0 +1,21 @@ +class Solution { + public int longestCommonSubsequence(String text1, String text2) { + int m = text1.length(); + int n = text2.length(); + + int[][] dp = new int[m+1][n+1]; + + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + // DP 계산을 +1로 했어야 했는데 이전 케이스를 재사용하는 -1로만 접근해서 범위 에러가 많이 났었다. + if (text1.charAt(i) == text2.charAt(j)) { + dp[i + 1][j + 1] = dp[i][j] + 1; + } else { + dp[i + 1][j + 1] = Math.max(dp[i][j + 1], dp[i + 1][j]); + } + } + } + + return dp[m][n]; + } +} From 7c6c91cf3af485ba9a9e6098910cd0f9d47775a5 Mon Sep 17 00:00:00 2001 From: jinvicky Date: Tue, 9 Sep 2025 23:05:18 +0900 Subject: [PATCH 2/3] longest common subsequence solution --- longest-common-subsequence/jinvicky.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 longest-common-subsequence/jinvicky.java diff --git a/longest-common-subsequence/jinvicky.java b/longest-common-subsequence/jinvicky.java new file mode 100644 index 000000000..6e9a63155 --- /dev/null +++ b/longest-common-subsequence/jinvicky.java @@ -0,0 +1,18 @@ +class Solution { + public int longestCommonSubsequence(String text1, String text2) { + int m = text1.length(); + int n = text2.length(); + int[][] dp = new int[m + 1][n + 1]; + + for (int i = 1; i <= m; i++) { + for (int j = 1; j <= n; j++) { + if (text1.charAt(i - 1) == text2.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1] + 1; // 문자가 같으면 대각선 값 + 1 + } else { + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); // 위 or 왼쪽 중 큰 값 + } + } + } + return dp[m][n]; + } +} From 8373128992aad2aad8ffd98fb2594cfe608345bd Mon Sep 17 00:00:00 2001 From: jinvicky Date: Tue, 9 Sep 2025 23:06:38 +0900 Subject: [PATCH 3/3] longest common subsequence solution --- longest-common-subsequence/jinvicky.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/longest-common-subsequence/jinvicky.java b/longest-common-subsequence/jinvicky.java index eb8876388..57a5da484 100644 --- a/longest-common-subsequence/jinvicky.java +++ b/longest-common-subsequence/jinvicky.java @@ -18,4 +18,23 @@ public int longestCommonSubsequence(String text1, String text2) { return dp[m][n]; } + + // -1을 참조해 이전 케이스를 재사용하는 경우는 1부터 m,n까지 하면 그렇게 할 수 있다. + public int longestCommonSubsequence2(String text1, String text2) { + int m = text1.length(); + int n = text2.length(); + int[][] dp = new int[m + 1][n + 1]; + + for (int i = 1; i <= m; i++) { + for (int j = 1; j <= n; j++) { + if (text1.charAt(i - 1) == text2.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1] + 1; // 문자가 같으면 대각선 값 + 1 + } else { + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); // 위 or 왼쪽 중 큰 값 + } + } + } + + return dp[m][n]; + } }