[ํ๋ก๊ทธ๋๋จธ์ค-Lv2] ์กฐ์ด์คํฑ / Python
๐ Problem Solving/Programmers
import sys
input = sys.stdin.readline
name = input().rstrip()
def solution(name):
change = [min(ord(i) - ord("A"), ord("Z") - ord(i) + 1) for i in name]
answer, idx = 0, 0
while True:
answer += change[idx]
change[idx] = 0
if sum(change) == 0:
return answer
left, right = 1, 1
while change[idx - left] == 0:
left += 1
while change[idx + right] == 0:
right += 1
answer += left if left < right else right
idx += -left if left < right else right
return answer
print(solution(name))
ํด์ค
change์ ์ ์ด๋๊ณผ ํ ์ด๋ ์ค ์ต์๋ฅผ ๋ฃ์ด์ค๋ค. ์ด ๋ ord๋ฅผ ์ฌ์ฉํด์ ์ค์ง์ ์ธ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ ์ ์๋๋ก ํ๋ค.
์ํ ์ด๋๊ฑฐ๋ฆฌ๋ฅผ answer์ ์ถ์ ์ํจ๋ค.
์ข์ฐ ์ด๋๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๊ณ , ๋ ์งง์ ๋ฐฉํฅ์ผ๋ก ์ด๋ํ๋ค. ์ด๋ํ ๊ฑฐ๋ฆฌ๋งํผ answer์ ์ถ์ ์ํจ๋ค.
์ข๋ก ์ด๋ํ๋ ๊ฑฐ๋ฆฌ๊ฐ ๋ ์งง์ผ๋ฉด answer์ ์ข๋ก ์ด๋ํ๋ ๊ฑฐ๋ฆฌ๋งํผ ์ถ์ .
์ฐ๋ก ์ด๋ํ๋ ๊ฑฐ๋ฆฌ๊ฐ ๋ ์งง์ผ๋ฉด answer์ ์ฐ๋ก ์ด๋ํ๋ ๊ฑฐ๋ฆฌ๋งํผ ์ถ์ .
์ข๋ก ์ด๋ํ๋ ๊ฑฐ๋ฆฌ๊ฐ ๋ ์งง์ผ๋ฉด idx๋ ํ์ฌ idx์ -left๋ฅผ ํด์ค๋ค.
์ฐ๋ก ์ด๋ํ๋ ๊ฑฐ๋ฆฌ๊ฐ ๋ ์งง์ผ๋ฉด idx๋ ํ์ฌ idx์ +right๋ฅผ ํด์ค๋ค.
'๐ Problem Solving > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค-Lv2] ๊ฐ์ฅ ํฐ ์ / Python (0) | 2021.06.14 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค-Lv2] ์์์ฐพ๊ธฐ / Python (0) | 2021.06.03 |
[ํ๋ก๊ทธ๋๋จธ์ค-Lv2] ํ๋ฆฐํฐ / Python (0) | 2021.06.02 |
[ํ๋ก๊ทธ๋๋จธ์ค-Lv2] ์ ํ๋ฒํธ ๋ชฉ๋ก / Python (0) | 2021.06.02 |
[ํ๋ก๊ทธ๋๋จธ์ค-Lv2] ํ๊ฒ ๋๋ฒ / Python (0) | 2021.06.02 |