Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[친환경사과] week9 #992

Merged
merged 2 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions linked-list-cycle/EcoFriendlyAppleSu.kt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

노드 값을 직접 변경해서 구하는 방법을 처음 봐서 신박하네요👍
원본데이터를 수정하지 않고 방문한 노드를 추적하는 방식으로도 풀어보시면 도움이 될 것 같아요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package leetcode_study

/*
* 링크드 리스트에서 순환이 발생하는지 체크하는 문제
* Node `val` 값을 주어진 범위 (-10,000 <= `val` <= 10,000) 보다 큰 정수로 변경해 cycle 판별 시도
* 시간 복잡도: O(n)
* -> linked list node 개수만큼 진행
* 공간 복잡도: O(1)
* -> 주어진 node를 가리키는 currentNode 이외에 추가되는 없음
* */
fun hasCycle(head: ListNode?): Boolean {
var currentNode = head

while (currentNode?.next != null) {
if (currentNode.`val` == 10001) return true // 이미 방문한 노드이면 사이클 존재
currentNode.`val` = 10001 // 방문한 노드 표시
currentNode = currentNode.next // 다음 노드로 이동
}

return false // `null`을 만났다면 사이클 없음
}
63 changes: 63 additions & 0 deletions longest-common-subsequence/EcoFriendlyAppleSu.kt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저번 주에 문제 중에 특히 어려웠던 문제로 기억하는데, bottom-up으로 효율적으로 푸셨네요🙂
공간 복잡도처럼 시간 복잡도도 text1과 text2의 길이를 사용한 O(nm)으로 표기할 수 있을 것 같아요
두 번째 풀이가 적합하지는 않지만 풀이를 시도한 과정을 볼 수 있어서 좋았습니다

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package leetcode_study

/*
* 가장 긴 공통 부분 문자열의 길이를 구하는 문제
* 동적 계획법을 사용한 문제 해결
* 문자가 동일할 경우, table[i][j] = table[i-1][j-1] + 1. 즉, 이전까지의 최장 공통 부분 문자열 길이에 1을 추가
* 문자가 다를 경우, table[i][j] = max(table[i-1][j], table[i][j-1]) 이는 현재까지 찾은 최장 공통 부분 문자열의 길이를 유지하는 과정
*
* 시간 복잡도: O(n^2)
* -> 두 분자열을 이중 반복을 진행하는 경우
* 공간 복잡도: O(nm) (= n과 m은 각각 주어진 문자열을 길이)
* -> dp table에 사용되는 공간
* */
fun longestCommonSubsequence(text1: String, text2: String): Int {
val n = text1.length
val m = text2.length
val dp = Array(n + 1) { IntArray(m + 1) }

for (i in 1..n) {
for (j in 1..m) {
if (text1[i - 1] == text2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1
} else {
dp[i][j] = maxOf(dp[i - 1][j], dp[i][j - 1])
}
}
}
return dp[n][m]
}

/*
* 주어진 두 문자열에 각각의 Index를 두어 비교해가며 해결 시도 해당 방법으로 시도
* Test case를 통과했지만 "bac", "abc"와 같은 case에서 "bc"를 답으로 도출할 수 있지만 "ac"와 같은 경우는 지나치게됨
* 즉, 정답이 되는 경우를 제외할 수 있음.
* */
fun longestCommonSubsequence(text1: String, text2: String): Int {
var result = 0
var longOne: String
var shortOne: String
var longIndex = 0
var shortIndex = 0

if (text1.length >= text2.length) {
longOne = text1
shortOne = text2
} else {
longOne = text2
shortOne = text1
}

while (shortIndex < shortOne.length) {
if (shortOne[shortIndex] == longOne[longIndex]) {
shortIndex += 1
longIndex += 1
result += 1
} else {
longIndex += 1
}
if (longIndex == longOne.length) break
}

return result
}