-
Notifications
You must be signed in to change notification settings - Fork 125
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
[친환경사과] week9 #992
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`을 만났다면 사이클 없음 | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저번 주에 문제 중에 특히 어려웠던 문제로 기억하는데, bottom-up으로 효율적으로 푸셨네요🙂 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
노드 값을 직접 변경해서 구하는 방법을 처음 봐서 신박하네요👍
원본데이터를 수정하지 않고 방문한 노드를 추적하는 방식으로도 풀어보시면 도움이 될 것 같아요!