https://school.programmers.co.kr/learn/courses/30/lessons/17677#
난이도: 레벨2
소요시간: 50분, 100점 못맞고 84.6점 맞음, 답지를 보고 다시 품
SET자료구조를 사용하는법
https://yunzae.tistory.com/133
1번쨰로 푼 코드
#16:55 level2 50분만에 푼건 84.6점
from collections import deque
def solution(str1, str2):
Alphabet=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",]
strList1=[]
strList2=[]
for i in range(len(str1)-1):
if str1[i].upper() in Alphabet and str1[i+1].upper() in Alphabet:
strList1.append(str1[i].upper()+str1[i+1].upper())
for i in range(len(str2)-1):
if str2[i].upper() in Alphabet and str2[i+1].upper() in Alphabet:
strList2.append(str2[i].upper()+str2[i+1].upper())
strgyoList1=strList1[:]
strgyoList2=strList2[:]
gyo=[]
while True:
gyoList=list(set(strgyoList1)&set(strgyoList2))
if not gyoList:
break
for i in range(len(gyoList)):
gyo.append(gyoList[i])
strgyoList1.remove(gyoList[i])
strgyoList2.remove(gyoList[i])
strhapList1=strList1[:]
strhapList2=strList2[:]
hap=[]
while True:
hapList=list(set(strhapList1)|set(strhapList2))
if not hapList:
break
for i in range(len(hapList)):
hap.append(hapList[i])
if hapList[i] in strhapList1:
strhapList1.remove(hapList[i])
if hapList[i] in strhapList2:
strhapList2.remove(hapList[i])
if len(gyo)==0 or len(hap)==0:
ans=1*65536
else:
ans= int(len(gyo)/len(hap)*65536)
return ans
2번째로 푼 코드, 카운트를 이용하여 복잡도를 낮추었다. 그리고 100점을 못맞은 이유를 찾았다.
마지막 답구하는 곳에서 교집합이 0이거나 합집합이 0 일때 1로 답을 반환했는데 교집합이 0이고 합집합이 0이면 답은 0이다.
그래서 분모인 합집합만 0이면 1인것이다.
문제에 교집합과 합집합이 동시에 0 인것으로 예시가 나와서 헷갈렸었다..
if hap==0:
ans=1*65536
else:
ans= int(gyo/hap*65536)
#16:55 level2 50분만에 푼건 84.6점
from collections import deque
def solution(str1, str2):
Alphabet=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
strList1=[]
strList2=[]
for i in range(len(str1)-1):
if str1[i].upper() in Alphabet and str1[i+1].upper() in Alphabet:
strList1.append(str1[i].upper()+str1[i+1].upper())
for i in range(len(str2)-1):
if str2[i].upper() in Alphabet and str2[i+1].upper() in Alphabet:
strList2.append(str2[i].upper()+str2[i+1].upper())
gyoList=list(set(strList1)&set(strList2))
hapList=list(set(strList1)|set(strList2))
gyo=0
hap=0
for i in range(len(gyoList)):
gyo+=min(strList1.count(gyoList[i]),strList2.count(gyoList[i]))
for i in range(len(hapList)):
hap+=max(strList1.count(hapList[i]),strList2.count(hapList[i]))
if hap==0:
ans=1*65536
else:
ans= int(gyo/hap*65536)
return ans
'Problem Solving > 자료구조' 카테고리의 다른 글
BOJ1406 에디터(리스트 이터레이터문제) *R (0) | 2024.03.06 |
---|---|
BOJ1158 요세푸스 문제 (0) | 2024.03.06 |
카카오 신고 결과 받기 (0) | 2023.05.04 |
kakao2018 캐시 (0) | 2023.04.10 |