Skip to content
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

56-xxubin04 #211

Merged
merged 2 commits into from
Sep 15, 2024
Merged

56-xxubin04 #211

merged 2 commits into from
Sep 15, 2024

Conversation

xxubin04
Copy link
Member

@xxubin04 xxubin04 commented Aug 4, 2024

πŸ”— 문제 링크

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ - λ‰΄μŠ€ ν΄λŸ¬μŠ€ν„°λ§


βœ”οΈ μ†Œμš”λœ μ‹œκ°„

20λΆ„


✨ μˆ˜λ„ μ½”λ“œ

1. 문제 이해

μžμΉ΄λ“œ μœ μ‚¬λ„ J(A, B) * 65536μ—μ„œ μ†Œμˆ˜μ  μ•„λž˜ μˆ˜λŠ” 버리고 좜λ ₯ν•˜λŠ” λ¬Έμ œμ΄λ‹€.


쑰건

  • λ¬Έμžμ—΄ str1, str2λ₯Ό μΈμ ‘ν•˜λŠ” 두 λ¬Έμžμ”© μž˜λΌμ„œ 집합을 κ΅¬μ„±ν•œλ‹€.

    • μ΄λ•Œ, 영문자λ₯Ό μ œμ™Έν•œ λ‹€λ₯Έ λ¬Έμžλ‚˜ 곡백이 λ“€μ–΄μžˆλ‹€λ©΄ μ œκ±°ν•œλ‹€.
    • λŒ€λ¬Έμžμ™€ μ†Œλ¬ΈμžλŠ” κ΅¬λΆ„ν•˜μ§€ μ•ŠλŠ”λ‹€.
    • μ˜ˆμ‹œ
    • aa1+aa2, AAAA12
    • ["AA", "AA"], ["AA", "AA", "AA"]
  • 집합 A와 집합 Bκ°€ λͺ¨λ‘ 곡집합인 κ²½μš°μ—λŠ” λ‚˜λˆ—μ…ˆμ΄ μ •μ˜λ˜μ§€ μ•ŠμœΌλ―€λ‘œ J(A,B) = 1둜 μ •μ˜ν•œλ‹€.

    • μ΄λ•Œ, μ£Όμ˜ν•΄μ•Ό ν•  점이 두 집합이 곡집합인 κ²½μš°μ—λ§Œ 두 μ§‘ν•©μ˜ 합집합 크기가 0이 λ˜λ―€λ‘œ λ‚˜λˆ—μ…ˆμ˜ λΆ„λͺ¨κ°€ 0이 λ˜λ―€λ‘œ λ‚˜λˆ—μ…ˆμ„ μ •μ˜ν•˜μ§€ λͺ»ν•œλ‹€λŠ” 것이닀. λ”°λΌμ„œ, 두 집합 λͺ¨λ‘κ°€ 곡집합 κ²½μš°μ—λ§Œ J(A, B)λ₯Ό 1둜 μ •μ˜ν•΄μ•Ό ν•œλ‹€.
    • 즉, 집합 Aλ‚˜ 집합 B 쀑 ν•˜λ‚˜λ§Œ 0인 κ²½μš°μ—λŠ” μœ„μ˜ 쑰건이 λ§Œμ‘±ν•˜μ§€ μ•ŠλŠ”λ‹€. κ·Έμ € J(A, B)λ₯Ό 0으둜 μ²˜λ¦¬ν•΄μ£Όλ©΄ λœλ‹€. μ™œλƒν•˜λ©΄, 두 μ§‘ν•©μ˜ ꡐ집합은 곡집합일 것이기 λ•Œλ¬Έμ΄λ‹€.
  • μžμΉ΄λ“œ μœ μ‚¬λ„ J(A, B) = 두 μ§‘ν•©μ˜ ꡐ집합 크기 / 두 μ§‘ν•©μ˜ 합집합 크기


2. μ½”λ“œ 뢄석

  answer = 0
      list1, list2 = [], []
      for a in range(len(str1)-1):
          if (slice_a := str1[a:a+2]).isalpha():
              list1.append(str1[a:a+2].upper())
      for b in range(len(str2)-1):
          if (slice_b := str2[b:b+2]).isalpha():
              list2.append(str2[b:b+2].upper())

λ¬Έμžμ—΄μ„ 두 κΈ€μžμ”© μž˜λΌμ„œ μ•ŒνŒŒλ²³μœΌλ‘œλ§Œ κ΅¬μ„±λœ κ²½μš°μ—λ§Œ 각각의 집합에 μΆ”κ°€ν•΄μ€€λ‹€.


    total = len(list1) + len(list2)
    duplication = 0

    for i in list1:
        if i in list2:
            list2.remove(i)
            duplication += 1

두 μ§‘ν•©μ˜ 크기의 합인 totalκ³Ό 두 μ§‘ν•©μ˜ κ΅μ§‘ν•©μ˜ 크기인 duplication을 μ •μ˜ν•œλ‹€.

집합 A(list1)의 각 μ›μ†Œκ°€ 집합 B(list2)에 μžˆλ‹€λ©΄, 집합 B에 ν•΄λ‹Ή μ›μ†Œλ₯Ό μ§€μ›Œμ£Όκ³  duplication에 1을 μΆ”κ°€ν•΄μ€€λ‹€. μ΄λ•Œ, 집합 B에 ν•΄λ‹Ή μ›μ†Œλ₯Ό μ§€μ›Œμ£ΌλŠ” μ΄μœ λŠ” ꡐ집합에 이미 μΆ”κ°€λœ μ›μ†Œλ‘œ μƒκ°ν•˜μ—¬μ•Ό ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€. μ’€ 더 μžμ„Έν•˜κ²Œ λ§ν•˜μžλ©΄, ["AA", "AA", "BB"], ["AA"]의 ꡐ집합은 ["AA"]이지, ["AA", "AA"]κ°€ μ•„λ‹ˆλΌλŠ” 것이닀. κ·ΈλŸ¬λ―€λ‘œ λ™μΌν•œ μ›μ†Œκ°€ λ‚˜νƒ€λ‚œλ‹€λ©΄, 두 집합 쀑 ν•˜λ‚˜μ˜ μ§‘ν•©μ—μ„œ ν•΄λ‹Ή μ›μ†Œλ₯Ό μ§€μ›Œμ•Ό ν•œλ‹€.


    if len(list2) == 0 or total == duplication:
        return 65536

    answer = floor(duplication / (total - duplication) * 65536)

이떄, μœ„μ—μ„œ μ–ΈκΈ‰ν•œ μ£Όμ˜ν•΄μ•Ό ν•  점을 μƒκΈ°ν•˜μ—¬μ•Ό ν•œλ‹€.

  • 집합 A와 집합 Bκ°€ λͺ¨λ‘ 곡집합인 κ²½μš°μ—λŠ” λ‚˜λˆ—μ…ˆμ΄ μ •μ˜λ˜μ§€ μ•ŠμœΌλ―€λ‘œ J(A,B) = 1둜 μ •μ˜ν•œλ‹€.
    • μ΄λ•Œ, μ£Όμ˜ν•΄μ•Ό ν•  점이 두 집합이 곡집합인 κ²½μš°μ—λ§Œ 두 μ§‘ν•©μ˜ 합집합 크기가 0이 λ˜λ―€λ‘œ λ‚˜λˆ—μ…ˆμ˜ λΆ„λͺ¨κ°€ 0이 λ˜λ―€λ‘œ λ‚˜λˆ—μ…ˆμ„ μ •μ˜ν•˜μ§€ λͺ»ν•œλ‹€λŠ” 것이닀. λ”°λΌμ„œ, 두 집합 λͺ¨λ‘κ°€ 곡집합 κ²½μš°μ—λ§Œ J(A, B)λ₯Ό 1둜 μ •μ˜ν•΄μ•Ό ν•œλ‹€.
    • 즉, 집합 Aλ‚˜ 집합 B 쀑 ν•˜λ‚˜λ§Œ 0인 κ²½μš°μ—λŠ” μœ„μ˜ 쑰건이 λ§Œμ‘±ν•˜μ§€ μ•ŠλŠ”λ‹€. κ·Έμ € J(A, B)λ₯Ό 0으둜 μ²˜λ¦¬ν•΄μ£Όλ©΄ λœλ‹€. μ™œλƒν•˜λ©΄, 두 μ§‘ν•©μ˜ ꡐ집합은 곡집합일 것이기 λ•Œλ¬Έμ΄λ‹€.

μ΄λ•Œ 문제λ₯Ό κ°€λ³κ²Œ 보고 μ§€λ‚˜κ°”λ”λ‹ˆ ν…ŒμŠ€νŠΈμΌ€μ΄μŠ€ 5μ—μ„œ ν‹€λ Έλ‹€κ³  λ–΄λ‹€. μ•Œκ³  λ΄€λ”λ‹ˆ 쑰건문을 λ‹€μŒκ³Ό 같이 μž‘μ„±ν•˜μ˜€κΈ° λ•Œλ¬Έμ΄μ—ˆλ‹€.

    if len(list1) == 0 or len(list2) == 0 or total == duplication:

어라? λ­”κ°€ λ¬Έμ œν’€λ•Œ κ·Έλƒ₯ len(list1) == 0을 μ§€μ› λ”λ‹ˆ ν†΅κ³Όν•΄μ„œ κ·Έλƒ₯ κ·Έλ ‡κ΅¬λ‚˜ν•˜κ³  μ§€λ‚˜κ°”λŠ”λ°, μ§€κΈˆ μƒκ°ν•΄λ³΄λ‹ˆ μš΄μ’‹κ²Œ ν…ŒμŠ€νŠΈμΌ€μ΄μŠ€λ₯Ό ν†΅κ³Όν•œ 것 κ°™λ„€μš”?

두 집합 λͺ¨λ‘ 곡집합인 κ²½μš°μ΄κ±°λ‚˜ 합집합과 κ΅μ§‘ν•©μ˜ 크기가 κ°™μ•„ J(A, B)κ°€ 1이 λ˜λŠ” κ²½μš°μ—λ§Œ 65536을 λ°˜ν™˜ν•΄μ•Ό ν•©λ‹ˆλ‹€. κ·Έλž˜μ„œ μ‹€μ œ 쑰건은 λ‹€μŒκ³Ό 같이 λ˜κ² λ„€μš”.

    if (len(list1) == 0 and len(list2) == 0) or total == duplication:
        return 65536

이 λΆ€λΆ„λ§Œ μˆ˜μ •ν•΄μ„œ λ‹€μ‹œ μ œμΆœν•΄λ³΄λ‹ˆ 잘 ν†΅κ³Όλ˜λ„€μš”~


3. 전체 μ½”λ“œ

from math import floor

def solution(str1, str2):
    answer = 0
    list1, list2 = [], []
    for a in range(len(str1)-1):
        if (slice_a := str1[a:a+2]).isalpha():
            list1.append(str1[a:a+2].upper())
    for b in range(len(str2)-1):
        if (slice_b := str2[b:b+2]).isalpha():
            list2.append(str2[b:b+2].upper())
    
    total = len(list1) + len(list2)
    duplication = 0

    for i in list1:
        if i in list2:
            list2.remove(i)
            duplication += 1
            
    if (len(list1) == 0 and len(list2) == 0) or total == duplication:
        return 65536
    
    answer = floor(duplication / (total - duplication) * 65536) 
        
    return answer

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

μ €λ²ˆμ— ꡐ황선배 ν‘Έμ‹  카카였 λ¬Έμ œλ„ 이런 κ²½μš°κ°€ μžˆμ—ˆλ˜ 것 같은데, 생각보닀 그런 κ²½μš°κ°€ λ§Žκ΅°μš”..?!
μ €λ²ˆμ£Όμ— μ•Œκ³ λ¦¬μ¦˜ λͺ» μ˜¬λ €μ„œ μ£„μ†‘ν•©λ‹ˆλ‹€.... λ‹€μ‹œ μ—΄μ‹¬νžˆ ν• κ²Œμš”..!πŸƒπŸ»

Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

list둜 데이터λ₯Ό μ €μž₯ν•˜λ©΄, μ›μ†Œ 쀑볡 검사 λ“±λ“±μ˜ λ‘œμ§μ„ μ²˜λ¦¬ν•˜λŠ” 데 μ‹œκ°„μ΄ 였래걸리죠. set을 μ‚¬μš©ν•˜λ©΄ $O(1)$ μ‹œκ°„μœΌλ‘œ ν•΄κ²°ν•  수 있겠죠?

m1 = Counter([str1[i:i+2].lower() for i in range(len(str1) - 1) if str1[i:i+2].isalpha()])
m2 = Counter([str2[i:i+2].lower() for i in range(len(str2) - 1) if str2[i:i+2].isalpha()])

Counter 라이브러리λ₯Ό μ‚¬μš©ν•˜λ©΄ μžλ™μœΌλ‘œ 개수 μΉ΄μš΄νŒ… κ²°κ³Όλ₯Ό λ”•μ…”λ„ˆλ¦¬ ν˜•νƒœλ‘œ λ°˜ν™˜ν•΄μ€λ‹ˆλ‹€.

intersection = sum((m1 & m2).values())
union = sum((m1 | m2).values())

νŒŒμ΄μ¬μ—μ„œλŠ” & μ—°μ‚°μžμ™€ | μ—°μ‚°μžλ₯Ό μ΄μš©ν•΄ ꡐ집합과 합집합을 ꡬ할 수 μžˆμŠ΅λ‹ˆλ‹€.

if union == 0:
    return 65536

합집합이 0μ΄λΌλŠ” 건 두 집합 κ°„ 곡톡 μ›μ†Œκ°€ μ—†λ‹€λŠ” λœ»μ΄λ‹ˆ, 1을 μ˜λ―Έν•˜λŠ” 65536을 λ°˜ν™˜ν•©λ‹ˆλ‹€.

similarity = intersection / union
return int(similarity * 65536)

μ•„λ‹ˆλΌλ©΄ μœ μ‚¬λ„λ₯Ό 계산해 65536을 κ³±ν•˜κ³  μ†Œμˆ˜μ μ„ 버린 값을 λ°˜ν™˜ν•˜λ©΄ 되죠.

전체 μ½”λ“œ

from collections import Counter

def solution(str1, str2):
    m1 = Counter([str1[i:i+2].lower() for i in range(len(str1) - 1) if str1[i:i+2].isalpha()])
    m2 = Counter([str2[i:i+2].lower() for i in range(len(str2) - 1) if str2[i:i+2].isalpha()])
    
    intersection = sum((m1 & m2).values())
    union = sum((m1 | m2).values())
    
    if union == 0:
        return 65536
    
    similarity = intersection / union
    return int(similarity * 65536)

이 문제 처음 봀을 λ•Œ μ„€λͺ…을 μ œλŒ€λ‘œ 이해 λͺ»ν–ˆλ˜ 기얡이 μžˆλ„€μš” γ…‹γ…‹

Copy link
Collaborator

@mjj111 mjj111 left a comment

Choose a reason for hiding this comment

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

python으둜 ν’€κΈ΄ν–ˆλŠ”λ°, 미리 μ„€κ³„μ•ˆν•˜κ³ 
ν‘ΈλŠ” 데둜 μ½”λ“œ μž‘μ„±ν•΄λ³΄λ‹ˆ 쑰금 λ”λŸ½λ„€μš”,..γ…Ž
νŒŒμ΄μ¬μ—μ„œ ꡐ집합 합집합은 set을 μ΄μš©ν•΄μ„œ μ•„λž˜μ™€ 같이 μ‚¬μš© κ°€λŠ₯ν•©λ‹ˆλ‹€ !

from math import floor

def solution(str1, str2):
    answer = 0
    str1, str2  = str1.lower(), str2.lower()
    str1_list, str2_list  = []. []

    for i in range(len(str1) - 1):
        if str1[i].isalpha() and str1[i+1].isalpha():
            str1_list.append(str1[i:i + 2])

    for i in range(len(str2) - 1):
        if str2[i].isalpha() and str2[i + 1].isalpha():
            str2_list.append(str2[i:i + 2])

    intersection_list = set(str1_list) & set(str2_list)
    union_list = set(str1_list) | set(str2_list)

    if len(union_list) == 0:
        return 65536

    intersection_len = sum([min(str1_list.count(intersection), str2_list.count(intersection)) for intersection in intersection_list])
    union_len = sum([max(str1_list.count(union), str2_list.count(union)) for union in union_list])

    answer = floor((intersection_len / union_len) * 65536)

    return answer

@9kyo-hwang 9kyo-hwang merged commit 65f6537 into AlgoLeadMe:main Sep 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants