-
Notifications
You must be signed in to change notification settings - Fork 0
뉴스 클러스터링 #35
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
Open
sangYuLv
wants to merge
1
commit into
main
Choose a base branch
from
liv
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
뉴스 클러스터링 #35
Changes from all commits
Commits
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 hidden or 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,34 @@ | ||
| // 프로그래머스 - 뉴스 클러스터링 | ||
|
|
||
| func solution(_ str1: String, _ str2: String) -> Int { | ||
| var arr1: [String] = toSplit(str1.lowercased()) | ||
| var arr2: [String] = toSplit(str2.lowercased()) | ||
|
|
||
| var intersection: Int = 0 | ||
|
|
||
| if arr1.isEmpty, arr2.isEmpty { return 65536 } | ||
|
|
||
| for item in arr1 { | ||
| if let idx = arr2.firstIndex(of: item) { | ||
| intersection += 1 | ||
| arr2.remove(at: idx) | ||
| } | ||
| } | ||
|
|
||
| let union = arr1.count + arr2.count | ||
| return intersection * 65536 / union | ||
| } | ||
|
|
||
| func toSplit(_ str: String) -> [String] { | ||
| var string = str | ||
| var result: [String] = [] | ||
| while !string.isEmpty { | ||
| var item: String = String(string.prefix(2)) | ||
| item.removeAll { !$0.isLetter } | ||
| if !item.isEmpty, item.count == 2 { | ||
| result.append(item) | ||
| } | ||
| string.removeFirst() | ||
| } | ||
| return result | ||
|
Comment on lines
+23
to
+33
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.
var result: [String] = []
let characters = Array(str)
if characters.count < 2 {
return result
}
for i in 0..<(characters.count - 1) {
let first = characters[i]
let second = characters[i+1]
if first.isLetter && second.isLetter {
result.append("\(first)\(second)")
}
}
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.
교집합을 계산하기 위해
arr1을 순회하면서arr2에서firstIndex(of:)와remove(at:)를 호출하는 방식은 O(N*M)의 시간 복잡도를 가집니다. 이는 배열의 크기가 클 때 비효율적입니다. 한쪽 배열을 빈도수 맵(Dictionary)으로 만들어두면 교집합을 O(N+M) 시간 복잡도로 찾을 수 있어 성능이 크게 향상됩니다. 또한, 합집합 계산 로직도 더 명확하게 표현할 수 있습니다.