Skip to content

뉴스 클러스터링#35

Open
sangYuLv wants to merge 1 commit intomainfrom
liv
Open

뉴스 클러스터링#35
sangYuLv wants to merge 1 commit intomainfrom
liv

Conversation

@sangYuLv
Copy link
Collaborator

@sangYuLv sangYuLv commented Feb 5, 2026

🔗 문제 링크

✔️ 소요된 시간

40분 + 30분

📚 새롭게 알게된 내용

  • 사전사전사전사전

@sangYuLv sangYuLv self-assigned this Feb 5, 2026
@gemini-code-assist
Copy link

Summary of Changes

Hello @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

  • 뉴스 클러스터링 문제 해결: 프로그래머스 레벨 2 '뉴스 클러스터링' 문제를 해결하는 Swift 코드가 추가되었습니다.
  • 자카드 유사도 계산 로직 구현: 두 입력 문자열에서 2글자씩 끊어 다중집합을 생성하고, 이 다중집합 간의 자카드 유사도를 계산하는 핵심 로직이 구현되었습니다.
  • 문자열 전처리 기능: toSplit 함수를 통해 입력 문자열을 소문자로 변환하고, 영문자로만 구성된 2글자 쌍을 추출하여 다중집합을 구성하는 전처리 과정을 수행합니다.
  • 다중집합 교집합 및 합집합 처리: 교집합을 계산할 때 한 번 사용된 요소는 제거하여 다중집합의 특성을 정확하게 반영하며, 이를 바탕으로 합집합의 크기를 계산합니다.
  • 예외 처리: 두 입력 문자열 모두에서 유효한 2글자 쌍이 생성되지 않아 다중집합이 모두 비어있는 경우, 문제에서 요구하는 대로 65536을 반환하도록 예외 처리가 포함되었습니다.

🧠 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
  • WEEK03/프로그래머스_뉴스클러스터링/Liv.swift
    • 새로운 Swift 파일이 추가되었습니다.
    • solution 함수는 두 문자열의 자카드 유사도를 계산하는 메인 로직을 포함합니다.
    • toSplit 함수는 입력 문자열을 2글자 단위의 영문자 쌍 다중집합으로 분리하는 유틸리티 기능을 제공합니다.
Activity
  • sangYuLv님이 프로그래머스 '뉴스 클러스터링' 문제 해결을 위한 Swift 코드를 추가했습니다.
  • 문제 해결에 총 70분(40분 + 30분)이 소요되었다고 언급했습니다.
  • 새롭게 알게 된 내용으로 '사전사전사전사전'을 기록했습니다.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a 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)으로 개선하여 훨씬 더 효율적인 코드로 만드는 데 중점을 둡니다.

Comment on lines +11 to +18
for item in arr1 {
if let idx = arr2.firstIndex(of: item) {
intersection += 1
arr2.remove(at: idx)
}
}

let union = arr1.count + arr2.count

Choose a reason for hiding this comment

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

high

교집합을 계산하기 위해 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

Comment on lines +23 to +33
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

Choose a reason for hiding this comment

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

high

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant