-
Notifications
You must be signed in to change notification settings - Fork 0
[1차] 뉴스 클러스터링 #34
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
base: main
Are you sure you want to change the base?
[1차] 뉴스 클러스터링 #34
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||
| func solution(_ str1: String, _ str2: String) -> Int { | ||||||
| let jac = jaccard(str1, str2) | ||||||
| return Int(jac * 65536) | ||||||
| } | ||||||
|
|
||||||
| // 자카드 유사도 | ||||||
| func jaccard(_ s1: String, _ s2: String) -> Double { | ||||||
| let set1 = makeSet(s1) | ||||||
| var set2 = makeSet(s2) | ||||||
|
|
||||||
| if set1.isEmpty && set2.isEmpty { return 1.0 } | ||||||
|
|
||||||
| var intersection: [String] = [] | ||||||
| var unionCount: Int = 0 | ||||||
|
|
||||||
| for element in set1 { | ||||||
| if let idx = set2.firstIndex(of: element) { | ||||||
| intersection.append(element) | ||||||
| set2.remove(at: idx) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| unionCount = set1.count + set2.count | ||||||
| return Double(intersection.count) / Double(unionCount) | ||||||
| } | ||||||
|
|
||||||
| // 집합 만들기 | ||||||
| func makeSet(_ string: String) -> [String] { | ||||||
| let chars = Array(string.lowercased()) | ||||||
| var result: [String] = [] | ||||||
|
|
||||||
| for i in 0..<chars.count-1 { | ||||||
|
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. The
Suggested change
|
||||||
| let c1 = chars[i] | ||||||
| let c2 = chars[i+1] | ||||||
|
|
||||||
| if c1.isLetter && c2.isLetter { | ||||||
| result.append(String([c1, c2])) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return result | ||||||
| } | ||||||
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.
jaccard함수에서 교집합과 합집합을 계산하는 현재 방식은 배열에 대해firstIndex(of:)와remove(at:)를 반복적으로 사용합니다. 이 배열 연산은 최악의 경우 O(N*M)의 시간 복잡도를 가질 수 있어, 입력 크기가 커질수록 성능 저하가 발생할 수 있습니다.makeSet함수에서 반환하는 딕셔너리(다중집합)를 활용하면, 교집합과 합집합을 O(N+M) 시간 복잡도로 효율적으로 계산할 수 있습니다. 제안된 코드는 다중집합의 합집합 공식인 |A ∪ B| = |A| + |B| - |A ∩ B|를 사용합니다.