https://school.programmers.co.kr/learn/courses/30/lessons/120875
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
나의 풀이
class Solution {
public int solution(int[][] dots) {
int x1 = dots[0][0];
int y1 = dots[0][1];
int x2 = dots[1][0];
int y2 = dots[1][1];
int x3 = dots[2][0];
int y3 = dots[2][1];
int x4 = dots[3][0];
int y4 = dots[3][1];
if ((double) (y1 - y2) / (x1 - x2) == (double) (y3 - y4) / (x3 - x4)) return 1;
if ((double) (y1 - y3) / (x1 - x3) == (double) (y2 - y4) / (x2 - x4)) return 1;
if ((double) (y1 - y4) / (x1 - x4) == (double) (y2 - y3) / (x2 - x3)) return 1;
return 0;
}
}
다른 풀이
import java.util.HashMap;
import java.util.Map;
class Solution {
public int solution(int[][] dots) {
Map<Double, Double> straightInfoMap = new HashMap<>();
for (int i = 0; i < dots.length - 1; i++) {
for (int j = i + 1; j < dots.length; j++) {
double slope = Math.abs((double) (dots[j][1] - dots[i][1]) / (double) (dots[j][0] - dots[i][0]));
double y_intercept = ((double) (dots[j][1]) - slope * dots[j][0]);
if (straightInfoMap.containsKey(slope) && straightInfoMap.get(slope) != y_intercept)
return 1;
straightInfoMap.put(slope, y_intercept);
}
}
System.out.println(straightInfoMap);
return 0;
}
}