1월 15일(월) - 3장 배열
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Solution {
private static class Point{
public 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 get_min_point(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 get_max_point(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 min = get_min_point(points);
Point max = get_max_point(points);
int width = (int)(max.x - min.x + 1);
int height = (int)(max.y - min.y + 1);
char[][] arr = new char[height][width];
for(char[] row : arr)
{
Arrays.fill(row,'.');
}
for(Point p : points)
{
int x = (int)(p.x - min.x);
int y = (int)(max.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;
}
}
댓글