1. ๋ฌธ์ ์ค๋ช
- ์
๋ ฅ :
- numbers [1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5] : ๋๋ฅผ ๋ฒํธ๋ฅผ ์์๋๋ก
- hand "right" : ์ค๋ฅธ์์ก์ด์ธ์ง, ์ผ์์ก์ด์ธ์ง
- ์ถ๋ ฅ :
- "LRLLLRLLRRL"
- 1,4,7 ์ ์ผ์์ผ๋ก ๋๋ฅด๊ณ , 3,6,9๋ ์ค๋ฅธ์์ผ๋ก ๋๋ฅธ๋ค.
- ๊ทธ๋ฆฌ๊ณ ๋๋จธ์ง ์ซ์๋ค์ ๋ ์์ง์๊ฐ๋ฝ์ ํ์ฌ ํคํจ๋์ ์์น์์ ๋ ๊ฐ๊น์ด ์์ง์๊ฐ๋ฝ์ ์ฌ์ฉ
- ๋ง์ฝ ๋ ์์ง์๊ฐ๋ฝ์ ๊ฑฐ๋ฆฌ๊ฐ ๊ฐ๋ค๋ฉด, ์ค๋ฅธ์์ก์ด๋ ์ค๋ฅธ์ ์์ง์๊ฐ๋ฝ, ์ผ์์ก์ด๋ ์ผ์ ์์ง์๊ฐ๋ฝ์ ์ฌ์ฉ
- ๊ฐ ๋ฒํธ(numbers)๋ฅผ ๋๋ฅธ ์์ง์๊ฐ๋ฝ์ด ์ผ์์ธ ์ง ์ค๋ฅธ์์ธ ์ง๋ฅผ ๋ํ๋ด๋ ์ฐ์๋ ๋ฌธ์์ด ํํ๋ก return
2. ์ฝ๋
solution1.py
def check_near_hand(current_position,num):
positions = {
1:(0,0),2:(0,1),3:(0,2),
4:(1,0),5:(1,1),6:(1,2),
7:(2,0),8:(2,1),9:(2,2),
'*':(3,0),0:(3,1),'#':(3,2)
}
left_hand = positions[current_position[0]]
right_hand = positions[current_position[1]]
num_position = positions[num]
#์ผ์๊ณผ Num์ ๊ฑฐ๋ฆฌ
left_distance = abs(left_hand[0] - num_position[0]) + abs(left_hand[1]-num_position[1])
#์ค๋ฅธ์๊ณผ Num์ ๊ฑฐ๋ฆฌ
right_distance = abs(right_hand[0] - num_position[0]) + abs(right_hand[1]-num_position[1])
if left_distance < right_distance:
return 'L'
elif left_distance > right_distance:
return 'R'
else:
return 'hand'
def solution(numbers, hand):
answer = ''
current_position = ['*','#']
left_key = [1,4,7]
right_key = [3,6,9]
for num in numbers:
if num in left_key:
current_position[0] = num
answer +='L'
elif num in right_key:
current_position[1] = num
answer +='R'
else:
if check_near_hand(current_position,num) == 'L':
current_position[0] = num
answer +='L'
elif check_near_hand(current_position,num) == 'R':
current_position[1] = num
answer +='R'
else:
if hand == 'left':
current_position[0] = num
answer +='L'
else:
current_position[1] = num
answer +='R'
return answer
solution2.py
def check_near_hand(l,r,num, hand):
positions = {
1:(0,0),2:(0,1),3:(0,2),
4:(1,0),5:(1,1),6:(1,2),
7:(2,0),8:(2,1),9:(2,2),
'*':(3,0),0:(3,1),'#':(3,2)
}
left_distance = abs(positions[l][0] - positions[num][0]) + abs(positions[l][1]-positions[num][1])
right_distance = abs(positions[r][0] - positions[num][0]) + abs(positions[r][1]-positions[num][1])
if left_distance == right_distance:
return 'L' if hand == 'left' else 'R'
return 'L' if left_distance < right_distance else 'R'
def solution(numbers, hand):
answer = ''
current_position = ['*','#']
left_key = [1,4,7]
right_key = [3,6,9]
for num in numbers:
if num in left_key:
current_position[0] = num
answer +='L'
elif num in right_key:
current_position[1] = num
answer +='R'
else:
if check_near_hand(current_position[0],current_position[1],num,hand) == 'L':
current_position[0] = num
answer +='L'
else:
current_position[1] = num
answer +='R'
return answer
3. ํ๊ณ
- ๊ฑฐ๋ฆฌ๊ณ์ฐ์ ์ด๋ป๊ฒ ํ ์ง๊ฐ ๊ฐ์ฅ ๊ณ ๋ฏผ์ด์๋ค.
- ๋์ ๋๋ฆฌ๋ก ์ขํ๋ฅผ ์ ํด์ฃผ๊ณ abs(์ผ์ ํ์ฌ์์น x๊ฐ - ๋๋ฅด๋ ค๋ ๋ฒํธ์์น x๊ฐ) + abs(์ผ์ ํ์ฌ์์น y๊ฐ - ๋๋ฅด๋ ค๋ ๋ฒํธ์์น y๊ฐ) ์ผ๋ก ์ผ์๊ณผ ๋๋ฅด๋ ค๋ ๋ฒํธ์ ๊ฑฐ๋ฆฌ, ์ค๋ฅธ์๋ ๋ง์น๊ฐ์ง๋ก ํด์ ๋์ ๋น๊ตํ๋ค.
- ์ฒซ ๋ฒ์งธ ํ์ด์์๋ ๊ฑฐ๋ฆฌ๊ณ์ฐ ๊ณผ์ ์์ ํท๊ฐ๋ ค์ ์ฌ๋ฌ ๋ณ์๋ฅผ ์ง์ ํ์ฌ ์ผ๋ค.
- ๊ทธ๋์ ๋ ๋ฒ์งธ ํ์ด์์๋ current_position ๋ฐฐ์ด ์์ฒด๋ณด๋ค ์ผ์ํ์ฌ์์น, ์ค๋ฅธ์ํ์ฌ์์น๋ฅผ ๋ฐ๋ก ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌํ๋ค.
- ๋ ๋ฒ์งธ ํ์ด๋ if ๋ฌธ์ด ๋๋ฌด ๋ง์ด ์ฐ์ธ ๊ฒ ๊ฐ์์ ํ์ค๋ก ์ ๋ฆฌํ๋ค.
- ์๊ณ ๋ฆฌ์ฆ์ด ๋ฌธ์ ์ ์ ํ์์ด์ ์ด๋ฅผ ๋ฐํ์ผ๋ก ํฐ ํ์ ์ก๊ณ ํ์ํ ํจ์๋ฅผ ๋์ค์ ์์ฑํด๋๊ฐ๋ ๊ฒ์ผ๋ก ๋ฐฉํฅ์ ์ก์ ๊ฒ์ด ๋์์ด ๋์๋ค.
'Algorithm > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ดํธ ๋ณํ (Python) (0) | 2022.01.06 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ฌธ์์ด ์์ถ (Python) (0) | 2022.01.04 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์คํจ์จ (Python) (0) | 2022.01.02 |
[ํ๋ก๊ทธ๋๋จธ์ค] [1์ฐจ] ๋น๋ฐ์ง๋ (Python) (0) | 2022.01.02 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์ซ์ ๋ฌธ์์ด๊ณผ ์๋จ์ด (Python) (0) | 2021.12.26 |