on my way

[백준 5622번] 다이얼 (Python3, 브론즈2) 본문

algorithm/Python

[백준 5622번] 다이얼 (Python3, 브론즈2)

wingbeat 2025. 1. 13. 21:23
반응형

https://www.acmicpc.net/problem/5622

 

# 리팩토링 전
import sys
from collections import defaultdict
dial = ["ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ"]
tel = defaultdict(int)
num = sys.stdin.readline().strip()
count = 0
for n in num:
    if not tel[n]:
        for i, d in enumerate(dial):
            if n in d:
                tel[n] = i+3
                break
    count += tel[n]
print(count)

# 리팩토링 후
import sys
dial = {"A":3, "B":3, "C":3,"D":4, "E":4, "F":4,"G":5, "H":5, "I":5,"J":6, "K":6, "L":6,"M":7, "N":7, "O":7,"P":8, "Q":8, "R":8, "S":8,"T":9, "U":9, "V":9,"W":10, "X":10, "Y":10, "Z":10}
num = sys.stdin.readline().strip()
count = 0
for n in num:
    count += dial[n]
print(count)

노가다하니 시간 복잡도는 빨라짐..ㅎㅎ

반응형