728x90
반응형
문제출처: https://school.programmers.co.kr/learn/courses/30/lessons/131128
풀이:
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
이거 왜 안되지?
반응형
댓글