본문 바로가기
프로그래머스/1단계

(Python/연습문제) 숫자 짝꿍

by windy7271 2022. 10. 7.
728x90
반응형

문제출처: https://school.programmers.co.kr/learn/courses/30/lessons/131128

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

풀이:

def solution(X, Y):
    arr1 = list(map(str,X))
    arr2 = list(map(str,Y))
    res = []
    while arr1:
        now = arr1.pop()
        if now in arr2:
            res.append(now)
            arr2.remove(now)
    res = [str(i) for i in sorted([int(i) for i in res], reverse=True)]
    return ''.join(res) if res else "-1"
# 63.2점 코드 
# 0 이 여러개인 경우를 안해줬고 시간초과도 난다

 

dic = {}
dic2 = {}
res = {}
for i in X:
    dic[i] = 0
for i in X:
    dic[i] += 1

for i in Y:
    dic2[i] = 0
for i in Y:
    dic2[i] += 1
for key,values in dic.items():
    if key in dic2:
        res[key] = min(values,dic2[key])
ans = []
for key,values in res.items():
    for i in range(values):
        ans.append(key)
print(ans)
if not ans:
    return "-1"
if all('0' == x for x in ans):
    return "0"
else:

for i in X:
    dic[i] = 0
    if dic[i] in dic:
        dic[i] += 1

이거 왜 안되지?

반응형

댓글