개발(0)/CodingTest

[프로그래머스/level0]평행 - 자바

까만밀가루 2024. 5. 7. 21:42

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

 


 

[문제 사고]

  1. 4개의 배열이기 때문에 직접 조합을 한다.
  2. 평행이 되기 위한 조건 : 기울기가 같다.

 

public int solution(int[][] dots) {
    int answer = 0;

    //제시된 배열 수가 적으므로 배열 경우의 수를 직접 작성
    int x1 = dots[0][0];
    int x2 = dots[1][0];
    int x3 = dots[2][0];
    int x4 = dots[3][0];

    int y1 = dots[0][1];
    int y2 = dots[1][1];
    int y3 = dots[2][1];
    int y4 = dots[3][1];

    //CASE 3가지 경우의 수
    double slope1 = (double) (x1 - x2) / (y1 - y2);
    double slope2 = (double) (x3 - x4) / (y3 - y4);
    if (slope1 == slope2) return answer = 1;

    slope1 = (double) (x1 - x3) / (y1 - y3);
    slope2 = (double) (x2 - x4) / (y2 - y4);
    if (slope1 == slope2) return answer = 1;

    slope1 = (double) (x1 - x4) / (y1 - y4);
    slope2 = (double) (x2 - x3) / (y2 - y3);
    if (slope1 == slope2) return answer = 1;

    return answer;
}

 


[테스트 통과 못한 케이스..]

프로그래머스 책 보기 전에 했던 것인데 .. 

프로그래머스 책 버전으로 바꿔봐도 아직도 미해결 한 것..

코드 실행시 성공으로 나오지만, 계속 테스트케이스12부터 실패한다.

혹시나 나중에 알 수 있을지도 모르니 일단 기록(누가 아시는 분은 알려주세요..)

 HashSet<Double> set = new HashSet<>();

 for(int i = 0 ; i < dots.length;i++){
     for(int j = i+1 ; j < dots.length;j++){
         double x = dots[i][0] - dots[j][0];
         double y = dots[i][1] - dots[j][1];

         double slope = x/y;
         if(!set.contains(slope)){
             set.add(slope);
         }else{
             answer = 1;
             break;
         }
     }
     if(answer == 1){
         break;
     }
 }

 return answer;