Recent Posts
Recent Comments
밍쎄의 코딩공간
프로그래머스 LV.2 - 교점에_별_만들기 본문
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Solution {
private static class Point {
public final long x,y;
private Point(long x, long y){
this.x = x;
this.y = y;
}
}
private Point intersection(long a1,long b1, long c1, long a2, long b2, long c2){
double x = (double) (b1 * c2 - b2 * c1)/ (a1 * b2 - a2 * b1);
double y = (double) (a2 * c1 - a1 * c2)/ (a1 * b2 - a2 * b1);
if (x % 1 !=0 || y % 1 != 0) return null;
return new Point((long)x, (long)y);
}
private Point getMinimumPoint(List<Point> points) {
long x = Long.MAX_VALUE;
long y = Long.MAX_VALUE;
for (Point p : points) {
if (p.x < x) x = p.x;
if (p.y < y) y = p.y;
}
return new Point(x,y);
}
private Point getMaximumPoint(List<Point> points) {
long x = Long.MIN_VALUE;
long y = Long.MIN_VALUE;
for (Point p : points) {
if (p.x > x) x = p.x;
if (p.y > y) y = p.y;
}
return new Point(x,y);
}
public String[] solution(int[][] line) {
List<Point> points = new ArrayList<>();
for (int i =0; i < line.length; i++) {
for(int j=i+1; j<line.length; j++) {
Point intersection = intersection (line[i][0],line[i][1],line[i][2],
line[j][0],line[j][1],line[j][2]);
if (intersection != null ){
points.add(intersection);
}
}
}
Point minimum = getMinimumPoint(points);
Point maximum = getMaximumPoint(points);
int width = (int) (maximum.x - minimum.x +1 );
int height = (int) (maximum.y - minimum.y +1 );
char [][] arr = new char [height][width];
for (char[] row : arr) {
Arrays.fill(row, '.');
}
for (Point p : points){
int x = (int) (p.x - minimum.x);
int y = (int) (maximum.y - p.y);
arr [y][x] = '*';
}
String[] result = new String[arr.length];
for(int i = 0; i < result.length; i++){
result[i]=new String(arr[i]);
}
return result;
}
}
https://school.programmers.co.kr/learn/courses/30/lessons/87377
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
그냥,.. 별 만들고 싶어서,,,
레벨 0은 이제 졸업할 때가 되었고 1을 중점적으로 파고들어야겠다.. 2를 풀면서 눈이 빠질뻔 했다!!~~~뿌듯
728x90
'프로그래머스 > 프로그래머스 LV.2' 카테고리의 다른 글
프로그래머스 LV.2 - 점 찍기 (0) | 2023.08.27 |
---|---|
프로그래머스 LV2 - 구명보트 (0) | 2023.08.27 |
프로그래머스 LV.2 - 광물캐기 (0) | 2023.08.25 |
프로그래머스 LV.2 - 삼각 달팽이 (0) | 2023.08.22 |
프로그래머스 LV.2 - 무인도 여행 (0) | 2023.08.20 |