본문 바로가기
Algorithm/문제풀이

[HackerRank] Diagonal Difference(Easy) by javascript

by 썸머워즈 2022. 3. 18.
반응형

 

해커랭크 스타트!


▷ 문제 : Diagonal Difference (Easy)

 

Diagonal Difference | HackerRank

Calculate the absolute difference of sums across the two diagonals of a square matrix.

www.hackerrank.com

▷ 해결 날짜 : 2022.03.18
▷ 소요 시간 : 5분
▷ 풀이 과정 :
Easy 난이도의 몸풀기용 문제라 그런지 많이 쉽다.

그냥 이중 배열을 전달받아서 좌상우하, 우상좌하 대각선 끼리 더한다음 두 값을 빼주는 문제이다.

 

결과는 절대값으로 반환하면 해결된다.


▷ 구현

function diagonalDifference(arr) {
    // Write your code here
    return arr.reduce((acc, cur, idx) => {
        acc[0] = acc[0] + cur[idx];
        acc[1] = acc[1] + cur[(cur.length - 1) - idx];
        return acc;
    }, [0, 0]).reduce((a, c) => Math.abs(a - c));
}

 

▷ 복기 :

프로그래머스 카카오 문제 풀다가 이런거 푸니까 나름 재밌네...

반응형


댓글

TOP