Skip to content

Commit 7f0cd73

Browse files
author
hangyeol
committed
97차 2번 문제 풀이
1 parent 6dd6ede commit 7f0cd73

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

live9/test97/문제2/백한결.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def solution(name):
2+
upDownmove = 0
3+
4+
# 조이스틱 위 아래 (최솟값)
5+
for spell in name:
6+
upDownmove += min(ord(spell) - ord('A'), ord('Z') - ord(spell) + 1)
7+
8+
# 조이스틱 오 왼
9+
# 조이스틱을 오른쪽으로만 계속 움직이는 경우
10+
rightLeftmove = len(name) - 1
11+
12+
# 조이스틱으로 오른쪽을 가다가 'A'를 만난 경우
13+
for i in range(len(name)):
14+
# 조이스틱 움직이는 횟수
15+
next = i + 1
16+
while len(name) > next and name[next] == 'A':
17+
next += 1
18+
19+
# 오른쪽으로 간 횟수, 오른쪽으로 가다가 A를 만나기 전까지의 움직임 횟수 중 최소의 2배 (앞으로 갔다가 뒤로 이동)
20+
# 오른쪽으로 간 횟수, 오른쪽으로 가다가 A를 만나기 전까지의 움직임 횟수 중 최대
21+
move = min(i, len(name) - next) * 2 + max(i, len(name) - next)
22+
rightLeftmove = min(rightLeftmove, move)
23+
24+
count = upDownmove + rightLeftmove
25+
26+
return count

0 commit comments

Comments
 (0)