Skip to content

Commit 3cb0dcd

Browse files
committed
[level 2] Title: 조이스틱, Time: 0.10 ms, Memory: 89.1 MB -BaekjoonHub
1 parent 2abc6bb commit 3cb0dcd

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# [level 2] 조이스틱 - 42860
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/42860)
4+
5+
### 성능 요약
6+
7+
메모리: 89.1 MB, 시간: 0.10 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 탐욕법(Greedy)
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2025년 09월 05일 14:42:40
20+
21+
### 문제 설명
22+
23+
<p>조이스틱으로 알파벳 이름을 완성하세요. 맨 처음엔 A로만 이루어져 있습니다.<br>
24+
ex) 완성해야 하는 이름이 세 글자면 AAA, 네 글자면 AAAA</p>
25+
26+
<p>조이스틱을 각 방향으로 움직이면 아래와 같습니다.</p>
27+
<div class="highlight"><pre class="codehilite"><code>▲ - 다음 알파벳
28+
▼ - 이전 알파벳 (A에서 아래쪽으로 이동하면 Z로)
29+
◀ - 커서를 왼쪽으로 이동 (첫 번째 위치에서 왼쪽으로 이동하면 마지막 문자에 커서)
30+
▶ - 커서를 오른쪽으로 이동 (마지막 위치에서 오른쪽으로 이동하면 첫 번째 문자에 커서)
31+
</code></pre></div>
32+
<p>예를 들어 아래의 방법으로 "JAZ"를 만들 수 있습니다.</p>
33+
<div class="highlight"><pre class="codehilite"><code>- 첫 번째 위치에서 조이스틱을 위로 9번 조작하여 J를 완성합니다.
34+
- 조이스틱을 왼쪽으로 1번 조작하여 커서를 마지막 문자 위치로 이동시킵니다.
35+
- 마지막 위치에서 조이스틱을 아래로 1번 조작하여 Z를 완성합니다.
36+
따라서 11번 이동시켜 "JAZ"를 만들 수 있고, 이때가 최소 이동입니다.
37+
</code></pre></div>
38+
<p>만들고자 하는 이름 name이 매개변수로 주어질 때, 이름에 대해 조이스틱 조작 횟수의 최솟값을 return 하도록 solution 함수를 만드세요.</p>
39+
40+
<h5>제한 사항</h5>
41+
42+
<ul>
43+
<li>name은 알파벳 대문자로만 이루어져 있습니다.</li>
44+
<li>name의 길이는 1 이상 20 이하입니다.</li>
45+
</ul>
46+
47+
<h5>입출력 예</h5>
48+
<table class="table">
49+
<thead><tr>
50+
<th>name</th>
51+
<th>return</th>
52+
</tr>
53+
</thead>
54+
<tbody><tr>
55+
<td>"JEROEN"</td>
56+
<td>56</td>
57+
</tr>
58+
<tr>
59+
<td>"JAN"</td>
60+
<td>23</td>
61+
</tr>
62+
</tbody>
63+
</table>
64+
<p><a href="https://commissies.ch.tudelft.nl/chipcie/archief/2010/nwerc/nwerc2010.pdf" target="_blank" rel="noopener">출처</a></p>
65+
66+
<p>※ 공지 - 2019년 2월 28일 테스트케이스가 추가되었습니다.<br>
67+
※ 공지 - 2022년 1월 14일 지문 수정 및 테스트케이스가 추가되었습니다. 이로 인해 이전에 통과하던 코드가 더 이상 통과하지 않을 수 있습니다.</p>
68+
69+
70+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public int solution(String name) {
3+
char[] names = name.toCharArray();
4+
int updown = 0;
5+
6+
// 알파벳 변경 횟수
7+
for(char ch : names) {
8+
updown += Math.min(ch - 'A', 'Z' - ch + 1);
9+
}
10+
11+
int len = name.length();
12+
int leftRight = len - 1;
13+
for(int i = 0; i < len; i++) {
14+
int next = i + 1;
15+
16+
// A가 몇개나 있는지 확인
17+
while(next < len && names[next] == 'A') {
18+
next++;
19+
}
20+
21+
// 1. 오른쪽 찍고 왼쪽으로 방향 전환
22+
leftRight = Math.min(i * 2 + (len - next), leftRight);
23+
24+
// 2. 왼쪽 찍고 오른쪽으로 방향 전환
25+
leftRight = Math.min((len - next) * 2 + i, leftRight);
26+
}
27+
28+
return leftRight + updown;
29+
}
30+
}

0 commit comments

Comments
 (0)