-
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
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @sangYuLv, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 이 PR은 프로그래머스 '뉴스 클러스터링' 문제 해결을 위한 Swift 코드를 추가합니다. 주요 목표는 두 문자열의 유사도를 자카드 유사도 방식으로 계산하는 것입니다. 이를 위해 문자열을 2글자 단위의 영문자 쌍 다중집합으로 변환하고, 이 다중집합들의 교집합과 합집합을 활용하여 최종 유사도 값을 도출합니다. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
뉴스 클러스터링 문제에 대한 솔루션이군요. 전체적인 로직은 맞지만, solution 함수와 toSplit 함수 모두에서 성능을 크게 개선할 여지가 있습니다. 두 함수 모두 현재 구현은 입력 크기에 따라 실행 시간이 급격히 늘어날 수 있는 O(N^2) 또는 O(N*M)의 시간 복잡도를 가집니다. 제안된 변경 사항은 시간 복잡도를 선형 시간 O(N+M)으로 개선하여 훨씬 더 효율적인 코드로 만드는 데 중점을 둡니다.
| for item in arr1 { | ||
| if let idx = arr2.firstIndex(of: item) { | ||
| intersection += 1 | ||
| arr2.remove(at: idx) | ||
| } | ||
| } | ||
|
|
||
| let union = arr1.count + arr2.count |
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) 시간 복잡도로 찾을 수 있어 성능이 크게 향상됩니다. 또한, 합집합 계산 로직도 더 명확하게 표현할 수 있습니다.
var map2 = arr2.reduce(into: [String: Int]()) { $0[$1, default: 0] += 1 }
for item in arr1 {
if let count = map2[item], count > 0 {
intersection += 1
map2[item]? -= 1
}
}
let union = arr1.count + arr2.count - intersection| 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 |
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.
while 루프 내에서 string.removeFirst()를 사용하는 현재 구현은 O(N^2)의 시간 복잡도를 가집니다. Swift에서 String의 맨 앞 문자를 제거하는 것은 문자열 길이에 비례하는 비용이 드는 연산이기 때문입니다. 이로 인해 긴 문자열이 입력될 경우 성능이 크게 저하될 수 있습니다. 문자열을 배열로 변환하고 인덱스를 통해 순회하는 방식으로 O(N) 시간 복잡도로 개선할 수 있습니다.
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
🔗 문제 링크
✔️ 소요된 시간
40분 + 30분
📚 새롭게 알게된 내용