slope List를 만들어준 후, 이중반복문으로 dots들의 기울기를 구한다.
Collections.frequency()를 이용하여 1이상이라면 기울기가 같은게 존재한다는 것이다.
import java.util.*;
public class 평행 {
public static int solution(int[][] dots) {
int answer = 0;
List<Double> slope = new ArrayList<>();
for (int i = 0; i < dots.length; i++) {
for (int j = i + 1; j < dots.length; j++) {
slope.add((double)(dots[i][1] - dots[j][1]) / (dots[i][0] - dots[j][0]));
}
}
Set<Double> set = new HashSet<>(slope);
for (Double s : set) {
if (Collections.frequency(slope, s) > 1) {
answer = 1;
}
}
return answer;
}
public static void main(String[] args) {
// int[][] dots = new int[][]{{1, 4}, {9, 2}, {3, 8}, {11, 6}};
int[][] dots = new int[][]{{3,5}, {4,1}, {2,4}, {5,10}};
int result = solution(dots);
System.out.println(result);
}
}
'Java > Programmers' 카테고리의 다른 글
[Lv.1]하샤드 수 (0) | 2023.01.19 |
---|---|
[Lv.0]옹알이(1) (0) | 2023.01.18 |
[Lv.0]겹치는 선분의 길이 (0) | 2023.01.18 |
[Lv.0]안전지대 (0) | 2023.01.18 |
[Lv.0]연속된 수의 합 (0) | 2023.01.18 |
댓글