Skip to content

Commit 85b055b

Browse files
committed
[level 2] Title: 조이스틱, Time: 0.23 ms, Memory: 85.9 MB -BaekjoonHub
1 parent 9d94836 commit 85b055b

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

프로그래머스/2/42860. 조이스틱/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### 성능 요약
66

7-
메모리: 89.1 MB, 시간: 0.10 ms
7+
메모리: 85.9 MB, 시간: 0.23 ms
88

99
### 구분
1010

@@ -16,7 +16,7 @@
1616

1717
### 제출 일자
1818

19-
2025년 09월 05일 14:42:40
19+
2025년 09월 13일 11:29:30
2020

2121
### 문제 설명
2222

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
1+
import java.util.*;
12
class Solution {
23
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);
4+
int upDown = 0;
5+
for(char ch : name.toCharArray()){
6+
upDown += Math.min(ch - 'A','Z' - ch + 1);
97
}
8+
System.out.println(upDown);
9+
10+
char[] names = name.toCharArray();
11+
int leftRight = name.length() - 1; // 기본값: 오른쪽으로 쭉 직진
12+
// 1. 왼쪽 찍고 오른쪽으로 다시 도착
13+
// 2. 오른쪽 찍고 왼쪽으로 다시 도착
14+
// 3. 왼쪽으로 쭉 직진 , 오른쪽으로 쭉 직진
1015

1116
int len = name.length();
12-
int leftRight = len - 1;
13-
for(int i = 0; i < len; i++) {
17+
for(int i = 0; i < len ; i++) {
1418
int next = i + 1;
1519

16-
// A가 몇개나 있는지 확인
17-
while(next < len && names[next] == 'A') {
18-
next++;
20+
while(next < len && names[next] == 'A'){
21+
next ++;
1922
}
2023

21-
// 1. 오른쪽 찍고 왼쪽으로 방향 전환
22-
leftRight = Math.min(i * 2 + (len - next), leftRight);
23-
24-
// 2. 왼쪽 찍고 오른쪽으로 방향 전환
25-
leftRight = Math.min((len - next) * 2 + i, leftRight);
24+
// 1번
25+
int temp1 = (len - next) * 2 + i;
26+
// 2번
27+
int temp2 = (i * 2) + len-next;
28+
leftRight = Math.min(leftRight,Math.min(temp1,temp2));
2629
}
2730

28-
return leftRight + updown;
31+
int answer = leftRight + upDown;
32+
return answer;
2933
}
3034
}

0 commit comments

Comments
 (0)