https://programmers.co.kr/learn/courses/30/lessons/49994#
코딩테스트 연습 - 방문 길이
programmers.co.kr
아마 이문제에서 가장 중요한 것은 점 단위(x, y)에 대한 중복을 체크하는 것이 아니라 경로에 대한 중복을 체크하는 것이라고 생각한다. (ex : (x,y) => (m,n)과 (m,n)=>(x,y)는 동일하므로 경로로 체크 X)
HashSet을 이용해서 라이브러리 자체적으로 중복을 제거한 후 길이를 리턴하면 된다고 생각하는데 아직 해결하지 못했다. 분명 테스트케이스들은 전부 해결했는데 좀 더 확인해봐야 할 것 같다.
완성코드와 관련 포스팅 : https://yoloaeee.tistory.com/60?category=954228
미완성 코드
import java.util.HashSet;
class Solution
{
public int solution(String dirs)
{
int currentX = 0;
int currentY = 0;
int lastX = 0;
int lastY = 0;
HashSet<String> set = new HashSet<String>();
for(int i=0; i<dirs.length(); i++)
{
currentX = lastX;
currentY = lastY;
if(dirs.charAt(i)=='U' && currentX+1<=5)
{
lastX++;
}
else if(dirs.charAt(i)=='D' && currentX-1>=-5)
{
lastX--;
}
else if(dirs.charAt(i)=='L' && currentY-1>=-5)
{
lastY--;
}
else if(dirs.charAt(i)=='R' && currentY+1<=5)
{
lastY++;
}
set.add(currentX + "" + currentY + "" + lastX + "" + lastY);
set.add(lastX + "" + lastY + "" + currentX + "" + currentY);
}
return set.size()/2;
}
}
'[ 다먹살 ] > - Coding' 카테고리의 다른 글
[구름] 레벨2 최단거리 (0) | 2021.08.01 |
---|---|
[구름] 레벨2 삼각형의 넓이2 (0) | 2021.07.30 |
[프로그래머스] 레벨2 게임 맵 최단거리 (0) | 2021.07.28 |
[프로그래머스] 레벨2 행렬의 곱셈 (0) | 2021.07.28 |
[프로그래머스] 레벨2 괄호 회전하기 (0) | 2021.07.27 |
댓글