반응형
풀이
- 간단하게 board의 길이에 따른 최대 크기를 mx에 담아둔 다음
- 방향에 따라 answer의 값을 추가 여기서 차감할 때는 abs 메서드를 사용해서 확인하기
class Solution {
public int[] solution(String[] keyinput, int[] board) {
int[] mx = new int[]{board[0]/2,board[1]/2};
int[] answer = new int[]{0,0};
for (String key: keyinput) {
if (key.equals("right") && mx[0] >= answer[0]+1) answer[0] += 1;
else if (key.equals("left") && mx[0] >= Math.abs(answer[0]-1)) answer[0] -= 1;
else if (key.equals("up") && mx[1] >= answer[1]+1) answer[1] += 1;
else if (key.equals("down") && mx[1] >= Math.abs(answer[1]-1)) answer[1] -= 1;
}
return answer;
}
}
'알고리즘' 카테고리의 다른 글
[프로그래머스, 자바] 배열 뒤집기, 문자열 뒤집기 (0) | 2024.03.16 |
---|---|
[프로그래머스, 자바] 외계어 사전 (0) | 2024.03.15 |
[프로그래머스, 자바] 로그인 성공? (0) | 2024.03.15 |
[프로그래머스, 자바] 다항식 더하기 (1) | 2024.03.15 |
[프로그래머스, 파이썬] 택배상자 (0) | 2024.03.14 |