Skip to content

Commit 6eb9dd8

Browse files
committed
[level 1] Title: [카카오 인턴] 키패드 누르기, Time: 0.62 ms, Memory: 16.3 MB -BaekjoonHub
1 parent a7fd530 commit 6eb9dd8

File tree

2 files changed

+247
-0
lines changed

2 files changed

+247
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
# [level 1] [카카오 인턴] 키패드 누르기 - 67256
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/67256)
4+
5+
### 성능 요약
6+
7+
메모리: 16.3 MB, 시간: 0.62 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 2020 카카오 인턴십
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2025년 10월 08일 17:46:18
20+
21+
### 문제 설명
22+
23+
<p>스마트폰 전화 키패드의 각 칸에 다음과 같이 숫자들이 적혀 있습니다.</p>
24+
25+
<p><img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/4b69a271-5f4a-4bf4-9ebf-6ebed5a02d8d/kakao_phone1.png" title="" alt="kakao_phone1.png"></p>
26+
27+
<p>이 전화 키패드에서 왼손과 오른손의 엄지손가락만을 이용해서 숫자만을 입력하려고 합니다.<br>
28+
맨 처음 왼손 엄지손가락은 <code>*</code> 키패드에 오른손 엄지손가락은 <code>#</code> 키패드 위치에서 시작하며, 엄지손가락을 사용하는 규칙은 다음과 같습니다.</p>
29+
30+
<ol>
31+
<li>엄지손가락은 상하좌우 4가지 방향으로만 이동할 수 있으며 키패드 이동 한 칸은 거리로 1에 해당합니다.</li>
32+
<li>왼쪽 열의 3개의 숫자 <code>1</code>, <code>4</code>, <code>7</code>을 입력할 때는 왼손 엄지손가락을 사용합니다.</li>
33+
<li>오른쪽 열의 3개의 숫자 <code>3</code>, <code>6</code>, <code>9</code>를 입력할 때는 오른손 엄지손가락을 사용합니다.</li>
34+
<li>가운데 열의 4개의 숫자 <code>2</code>, <code>5</code>, <code>8</code>, <code>0</code>을 입력할 때는 두 엄지손가락의 현재 키패드의 위치에서 더 가까운 엄지손가락을 사용합니다.<br>
35+
4-1. 만약 두 엄지손가락의 거리가 같다면, 오른손잡이는 오른손 엄지손가락, 왼손잡이는 왼손 엄지손가락을 사용합니다.</li>
36+
</ol>
37+
38+
<p>순서대로 누를 번호가 담긴 배열 numbers, 왼손잡이인지 오른손잡이인 지를 나타내는 문자열 hand가 매개변수로 주어질 때, 각 번호를 누른 엄지손가락이 왼손인 지 오른손인 지를 나타내는 연속된 문자열 형태로 return 하도록 solution 함수를 완성해주세요.</p>
39+
40+
<h5><strong>[제한사항]</strong></h5>
41+
42+
<ul>
43+
<li>numbers 배열의 크기는 1 이상 1,000 이하입니다.</li>
44+
<li>numbers 배열 원소의 값은 0 이상 9 이하인 정수입니다.</li>
45+
<li>hand는 <code>"left"</code> 또는 <code>"right"</code> 입니다.
46+
47+
<ul>
48+
<li><code>"left"</code>는 왼손잡이, <code>"right"</code>는 오른손잡이를 의미합니다.</li>
49+
</ul></li>
50+
<li>왼손 엄지손가락을 사용한 경우는 <code>L</code>, 오른손 엄지손가락을 사용한 경우는 <code>R</code>을 순서대로 이어붙여 문자열 형태로 return 해주세요.</li>
51+
</ul>
52+
53+
<hr>
54+
55+
<h5><strong>입출력 예</strong></h5>
56+
<table class="table">
57+
<thead><tr>
58+
<th>numbers</th>
59+
<th>hand</th>
60+
<th>result</th>
61+
</tr>
62+
</thead>
63+
<tbody><tr>
64+
<td>[1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5]</td>
65+
<td><code>"right"</code></td>
66+
<td><code>"LRLLLRLLRRL"</code></td>
67+
</tr>
68+
<tr>
69+
<td>[7, 0, 8, 2, 8, 3, 1, 5, 7, 6, 2]</td>
70+
<td><code>"left"</code></td>
71+
<td><code>"LRLLRRLLLRR"</code></td>
72+
</tr>
73+
<tr>
74+
<td>[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]</td>
75+
<td><code>"right"</code></td>
76+
<td><code>"LLRLLRLLRL"</code></td>
77+
</tr>
78+
</tbody>
79+
</table>
80+
<h5><strong>입출력 예에 대한 설명</strong></h5>
81+
82+
<p><strong>입출력 예 #1</strong></p>
83+
84+
<p>순서대로 눌러야 할 번호가 [1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5]이고, 오른손잡이입니다.</p>
85+
<table class="table">
86+
<thead><tr>
87+
<th>왼손 위치</th>
88+
<th>오른손 위치</th>
89+
<th>눌러야 할 숫자</th>
90+
<th>사용한 손</th>
91+
<th>설명</th>
92+
</tr>
93+
</thead>
94+
<tbody><tr>
95+
<td>*</td>
96+
<td>#</td>
97+
<td>1</td>
98+
<td>L</td>
99+
<td>1은 왼손으로 누릅니다.</td>
100+
</tr>
101+
<tr>
102+
<td>1</td>
103+
<td>#</td>
104+
<td>3</td>
105+
<td>R</td>
106+
<td>3은 오른손으로 누릅니다.</td>
107+
</tr>
108+
<tr>
109+
<td>1</td>
110+
<td>3</td>
111+
<td>4</td>
112+
<td>L</td>
113+
<td>4는 왼손으로 누릅니다.</td>
114+
</tr>
115+
<tr>
116+
<td>4</td>
117+
<td>3</td>
118+
<td>5</td>
119+
<td>L</td>
120+
<td>왼손 거리는 1, 오른손 거리는 2이므로 왼손으로 5를 누릅니다.</td>
121+
</tr>
122+
<tr>
123+
<td>5</td>
124+
<td>3</td>
125+
<td>8</td>
126+
<td>L</td>
127+
<td>왼손 거리는 1, 오른손 거리는 3이므로 왼손으로 8을 누릅니다.</td>
128+
</tr>
129+
<tr>
130+
<td>8</td>
131+
<td>3</td>
132+
<td>2</td>
133+
<td>R</td>
134+
<td>왼손 거리는 2, 오른손 거리는 1이므로 오른손으로 2를 누릅니다.</td>
135+
</tr>
136+
<tr>
137+
<td>8</td>
138+
<td>2</td>
139+
<td>1</td>
140+
<td>L</td>
141+
<td>1은 왼손으로 누릅니다.</td>
142+
</tr>
143+
<tr>
144+
<td>1</td>
145+
<td>2</td>
146+
<td>4</td>
147+
<td>L</td>
148+
<td>4는 왼손으로 누릅니다.</td>
149+
</tr>
150+
<tr>
151+
<td>4</td>
152+
<td>2</td>
153+
<td>5</td>
154+
<td>R</td>
155+
<td>왼손 거리와 오른손 거리가 1로 같으므로, 오른손으로 5를 누릅니다.</td>
156+
</tr>
157+
<tr>
158+
<td>4</td>
159+
<td>5</td>
160+
<td>9</td>
161+
<td>R</td>
162+
<td>9는 오른손으로 누릅니다.</td>
163+
</tr>
164+
<tr>
165+
<td>4</td>
166+
<td>9</td>
167+
<td>5</td>
168+
<td>L</td>
169+
<td>왼손 거리는 1, 오른손 거리는 2이므로 왼손으로 5를 누릅니다.</td>
170+
</tr>
171+
<tr>
172+
<td>5</td>
173+
<td>9</td>
174+
<td>-</td>
175+
<td>-</td>
176+
<td></td>
177+
</tr>
178+
</tbody>
179+
</table>
180+
<p>따라서 <code>"LRLLLRLLRRL"</code>를 return 합니다.</p>
181+
182+
<p><strong>입출력 예 #2</strong></p>
183+
184+
<p>왼손잡이가 [7, 0, 8, 2, 8, 3, 1, 5, 7, 6, 2]를 순서대로 누르면 사용한 손은 <code>"LRLLRRLLLRR"</code>이 됩니다.</p>
185+
186+
<p><strong>입출력 예 #3</strong></p>
187+
188+
<p>오른손잡이가 [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]를 순서대로 누르면 사용한 손은 <code>"LLRLLRLLRL"</code>이 됩니다.</p>
189+
190+
191+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import Foundation
2+
3+
func solution(_ numbers:[Int], _ hand:String) -> String {
4+
var answer = ""
5+
var touchHand: [String: [Int]] = [
6+
"left":[3,0], "right":[3,2]
7+
]
8+
9+
var num = 1
10+
var keypad = [Int: [Int]]()
11+
12+
for i in 0..<3 {
13+
for j in 0..<3 {
14+
keypad[num, default: []] = [i,j]
15+
num += 1
16+
}
17+
}
18+
19+
keypad[0] = [3, 1]
20+
21+
22+
for number in numbers {
23+
if number == 1 || number == 4 || number == 7 {
24+
answer.append("L")
25+
touchHand["left"] = keypad[number]!
26+
} else if number == 3 || number == 6 || number == 9 {
27+
answer.append("R")
28+
touchHand["right"] = keypad[number]!
29+
} else { // 가운데 숫자
30+
let numberLocation = keypad[number]!
31+
let leftLocation = touchHand["left"]!
32+
let rightLocation = touchHand["right"]!
33+
34+
let distanceLeft = abs(numberLocation[0] - leftLocation[0]) + abs(numberLocation[1] - leftLocation[1])
35+
let distanceRight = abs(numberLocation[0] - rightLocation[0]) + abs(numberLocation[1] - rightLocation[1])
36+
37+
if distanceLeft < distanceRight {
38+
answer.append("L")
39+
touchHand["left"] = numberLocation
40+
} else if distanceLeft > distanceRight {
41+
answer.append("R")
42+
touchHand["right"] = numberLocation
43+
} else { // 거리가 동일할 때 -> 어느 손잡이 인지에 따라 따라감
44+
if hand == "right" {
45+
answer.append("R")
46+
touchHand["right"] = numberLocation
47+
} else {
48+
answer.append("L")
49+
touchHand["left"] = numberLocation
50+
}
51+
}
52+
}
53+
}
54+
55+
return answer
56+
}

0 commit comments

Comments
 (0)