본문 바로가기
알고리즘/프로그래머스 level 1,2

[파이썬🐍] 프로그래머스 : 조이스틱

by 코딩개미뚠뚠 2021. 5. 28.
반응형
def solution(name):
    short_map=[min(ord(i)-ord("A"), ord("Z")-ord(i)+1) for i in name]
    idx, answer = 0, 0
    while True:
        answer += short_map[idx]
        short_map[idx] = 0
        if sum(short_map) == 0:
            return answer
        left, right = 1, 1
        while short_map[idx-left] == 0:
            left += 1
        while short_map[idx+right] == 0:
            right += 1
        answer += left if left < right else right
        idx += -left if left < right else right
반응형

댓글