Lewis's Tech Keep

[프로그래머스] 방문 길이 ( 2 try) 본문

JAVA/알고리즘

[프로그래머스] 방문 길이 ( 2 try)

Lewis Seo 2021. 2. 6. 04:19

 - formula 만들어서 하는 방식 : 실행시간 효율적, 공간 적게 차지 

 - 그러나 이해하기 힘든 단점 + 로직적으로 문제와 잘 안 물리는 부분이 있음 (문제는 거리 -> path로 변경하기 때문)

 - 나중에 생각해보면 boolean으로 하려면 4차원 배열이 오히려 가장 직관적일 수도 있음

 - 객체 지향으로 풀었을 때는 아래의 답이 깔끔하게 나오는 것 중 하나라고 생각함

 

더보기
import java.util.*;
class Solution {
    class Point {
        int x;
        int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() {
            return this.x;
        }

        public int getY() {
            return this.y;
        }

        public void move(int x, int y) {
            this.x += x;
            this.y += y;
        }

        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }

        @Override
		public boolean equals(Object o) {
			Point point = (Point) o;
			return this.x == point.x && this.y == point.y;
		}
    }

    class Direction {
        private int x = 0;
        private int y = 0;

        public Direction(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() {
            return this.x;
        }

        public int getY() {
            return this.y;
        }
    }

    class Path {
        Point cp;
        Point np;

        public Path(Point cp, Point np) {
            this.cp = cp;
            this.np = np;
        }
        
		@Override
		public int hashCode() {
			return Objects.hash(cp, np);
		}

		@Override
		public boolean equals(Object o) {
			Path path = (Path) o;
			return this.cp.equals(path.cp) && this.np.equals(path.np);
		}
    }
    
    public int solution(String dirs) {
        int answer = 0;
        
        Set<Path> pathSet = new HashSet<>();
        
        Point nowPoint = new Point(0, 0);
        
        Map<String, Direction> dMap = new HashMap<>();
        dMap.put("U", new Direction(0, 1));
        dMap.put("D", new Direction(0, -1));
        dMap.put("L", new Direction(-1, 0));
        dMap.put("R", new Direction(1, 0));
        
        for(int i=0; i<dirs.length(); i++) {
            String dir = String.valueOf(dirs.charAt(i));
            Direction d = dMap.get(dir);
            int x = nowPoint.getX();
            int y = nowPoint.getY();
            int dx = d.getX();
            int dy = d.getY();
            int nx = x + dx;
            int ny = y + dy;
            
            if(validMove(nx, ny)) {
                Point nextPoint = new Point(nx, ny);
                Path p = new Path(nowPoint, nextPoint);
                Path p2 = new Path(nextPoint, nowPoint);
                if(!pathSet.contains(p) && !pathSet.contains(p2)) {
                    pathSet.add(p);
                    pathSet.add(p2);
                    answer++;
                }
                
                nowPoint = nextPoint;
            }
        }
        
        return answer;
    }
    
    public boolean validMove(int nx, int ny) {
        int minRange = -5;
        int maxRange = 5;
        return (nx >= minRange && nx <= maxRange && ny >= minRange && ny <= maxRange);
    }
}

'JAVA > 알고리즘' 카테고리의 다른 글

[프로그래머스] 올바른 괄호  (0) 2021.02.09
[프로그래머스] N으로 표현  (0) 2021.02.08
[프로그래머스] 방문 길이  (0) 2021.02.05
[프로그래머스] H-Index  (0) 2021.02.03
[프로그래머스] 여행 경로  (0) 2021.02.02
Comments