[Programmers] 교점에 별 만들기

2023. 8. 16. 19:10Computer Sciences/Problem Solve

https://school.programmers.co.kr/learn/courses/30/lessons/87377

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제 설명

수학을 조금 응용한 구현 문제이다. 문제에서 교점을 구하는 공식을 제공하기 때문에 그대로 구현하기만 하면 된다. 주의할 점은 배열에서 1사분면과 2사분면, 3사분면과 4사분면의 위치를 반전시키기 위해 y = maxY - i.x, x = i.x - minX로 해주어야 한다. 예를 들어 maxX =4, maxY = 4이고 (1, 1)의 점이라고 가정하고 둘다 max에서 빼기로 진행하는 경우 y = 3, x = 3이므로 2사분면에 점이 찍힌다.

코드

import java.util.Set;
import java.util.HashSet;

class Solution {
    public String[] solution(int[][] lines) {
        Set<Intersection> set = new HashSet<>();
        
        long minX = Long.MAX_VALUE;
        long minY = Long.MAX_VALUE;
        long maxX = Long.MIN_VALUE;
        long maxY = Long.MIN_VALUE;
        
        for (int i = 0; i < lines.length - 1; i++) {
            int[] line = lines[i];
            double A = line[0], B = line[1], E = line[2];
            
            for (int j = i + 1; j < lines.length; j++) {
                int[] nextLine = lines[j];
                double C = nextLine[0], D = nextLine[1], F = nextLine[2];
                
                if (A * D - B * C == 0) {
                    continue;
                }
                
                double x = (B * F - E * D) / (A * D - B * C);
                if (x != (long) x) {
                    continue;
                }
                
                double y = (E * C - A * F) / (A * D - B * C);
                if (y != (long) y) {
                    continue;
                }
                
                set.add(new Intersection((long) x, (long) y));
                
                if (minX > x) {
                    minX = (long) x;
                }
                
                if (minY > y) {
                    minY = (long) y;
                }
                
                if (maxX < x) {
                    maxX = (long) x;
                }
                
                if (maxY < y) {
                    maxY = (long) y;
                }
            }
        }

        long height = maxY - minY + 1;
        long width = maxX - minX + 1;
        boolean[][] starMap = new boolean[(int) height][(int) width];
        String[] answer = new String[(int) height];
        
        for (Intersection i: set) {
            int x = (int)(i.x - minX);
            int y = (int)(maxY - i.y);
            
            starMap[y][x] = true;
        }
        
        for (int y = 0; y < height; y++) {
            StringBuilder sb = new StringBuilder();
            for (int x = 0; x < width; x++) {
                sb.append(starMap[y][x] ? "*" : ".");
            }
            answer[y] = sb.toString();
        }
        
        return answer;
    }
    
    static class Intersection {
        long x;
        long y;
        
        public Intersection(long x, long y) {
            this.x = x;
            this.y = y;
        }
    }
}

'Computer Sciences > Problem Solve' 카테고리의 다른 글

[Programmers] 튜플  (0) 2023.08.17
[Programmers] 할인 행사  (0) 2023.08.17
[Programmers] [1차] 캐시  (0) 2023.08.16
[Programmers] H-Index  (0) 2023.08.10
[Programmers] n^2 배열 자르기  (0) 2023.08.10