Skip to content

Instantly share code, notes, and snippets.

@aliwo
Created September 13, 2019 03:25
Show Gist options
  • Save aliwo/1ad807b3aebe3724f8428e8809508f63 to your computer and use it in GitHub Desktop.
Save aliwo/1ad807b3aebe3724f8428e8809508f63 to your computer and use it in GitHub Desktop.
def alter_alpha(to):
num = ord(to)
return num - 65 if num < 79 else 90 - num + 1
def traverse(done, i):
# TODO ; left_cnt 보다 right_cnt 가 크면 right 탐색을 중단 하는 최적화가 가능할까? 답에 영향이 없을까?
# 기저 사례
if False not in done:
return 0
left_cnt = right_cnt = 0
left_i = right_i = i
for _ in range(len(done)):
if done[left_i] == False:
break
left_i = left_i - 1 if left_i > 0 else len(done) - 1
left_cnt += 1
done[left_i] = True
left_cnt += traverse(done, left_i)
done[left_i] = False
for _ in range(len(done)):
if done[right_i] == False:
break
right_i = right_i + 1 if right_i + 1 < len(done) else 0
right_cnt += 1
done[right_i] = True
right_cnt += traverse(done, right_i)
done[right_i] = False
return min(left_cnt, right_cnt)
def solution(name):
'''
done 을 쓰지 않고 해결할 방법은 없을까?
'''
cnt = 0
done = [False] * len(name)
for i in range(len(name)):
if name[i] == 'A':
done[i] = True
else:
cnt += alter_alpha(name[i])
return cnt + traverse(done, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment