본문 바로가기
Programmers/Lv.0

[프로그래머스][Js] Lv.0 - 평행

by pin9___9 2023. 3. 13.
728x90

문제💭


 

나의 풀이👨‍💻

function solution(dots) {
    let result = []
    for (let i = 0; i < dots.length; i++) {
        for (let j = i + 1; j < dots.length; j++) {
            result.push((dots[j][0] - dots[i][0]) / (dots[j][1] - dots[i][1]))
        }
    }
    return result.length !== [...new Set(result)].length ? 1 : 0
}

 


다른 사람의 풀이👨‍🏫

1. 

function solution(dots) {
    if (calculateSlope(dots[0], dots[1]) === calculateSlope(dots[2], dots[3]))
        return 1;
    if (calculateSlope(dots[0], dots[2]) === calculateSlope(dots[1], dots[3]))
        return 1;
    if (calculateSlope(dots[0], dots[3]) === calculateSlope(dots[1], dots[2]))
        return 1;
    return 0;
}

function calculateSlope(arr1, arr2) {
    return (arr2[1] - arr1[1]) / (arr2[0] - arr1[0]);
}

2. 

var m=([a,b],[c,d])=>(d-b)/(c-a),f=(a,b,c,d)=>m(a,b)==m(c,d),solution=([a,b,c,d])=>(f(a,b,c,d)||f(a,c,b,d)||f(a,d,b,c))*1

 


Reference

  • 프로그래머스
 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

728x90

댓글