KotlinAlgorithm#23 (POG 평행)

박채빈·2023년 7월 23일
0

KotlinAlgorithm

목록 보기
23/28
post-thumbnail

POG 평행

링크

코드

class Solution {
    private fun calc(dot1: IntArray, dot2: IntArray): Float {
        val diffX = abs(dot1[0] - dot2[0]).toFloat()
        val diffY = abs(dot1[1] - dot2[1]).toFloat()
        return diffY / diffX
    }

    fun solution(dots: Array<IntArray>): Int {
        // 01 / 23
        if (calc(dots[0], dots[1]) == calc(dots[2], dots[3])) return 1

        // 02 / 13
        if (calc(dots[0], dots[2]) == calc(dots[1], dots[3])) return 1

        // 03 / 12
        if (calc(dots[0], dots[3]) == calc(dots[1], dots[2])) return 1

        return 0
    }
}

점 4개 고정이라 그냥 선분 3세트로 묶어서 기울기 비교함

profile
안드로이드 개발자

1개의 댓글

comment-user-thumbnail
2023년 7월 23일

감사합니다. 이런 정보를 나눠주셔서 좋아요.

답글 달기