일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Algorithms
- 블록체인
- 정규표현식
- 최강의 인생
- 코딩테스트
- join
- 개발자의 책장
- array.slice()
- 자바스크립트
- JavaScript
- Hackerrank
- MySQL
- 알고리즘
- for문
- 재귀함수
- 개발자_조이킴
- 코드스테이츠
- 코플릿
- 배열
- array
- 코딩공부
- Programmers
- SQL
- node.js
- 역행자
- 프로그래머스
- array.push()
- Developer_JoyKim
- Where
- select
Archives
- Today
- Total
CodingSpace
[HackerRank/Algorithms] Warmup - Diagonal Difference 본문
HackerRank/Algorithm
[HackerRank/Algorithms] Warmup - Diagonal Difference
개발자_조이킴 2022. 8. 21. 00:04Problem. Warmup - Diagonal Difference
Link.
https://www.hackerrank.com/challenges/diagonal-difference/problem?isFullScreen=true
A Very Big Sum | HackerRank
Calculate the sum of the values in an array that might exceed the range of int values.
www.hackerrank.com
Description.
Given a square matrix, calculate the absolute difference between the sums of its diagonals.
Key Point.
My Answer.
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'diagonalDifference' function below.
*
* The function is expected to return an INTEGER.
* The function accepts 2D_INTEGER_ARRAY arr as parameter.
*/
function diagonalDifference(arr) {
// Write your code here
let leftSum = 0;
let rightSum = 0;
for(let i = 0; i < arr.length; i++) {
let left = arr[i][i];
let right = arr[i][arr.length - i - 1]
leftSum = leftSum + left;
rightSum = rightSum + right;
}
return Math.abs(leftSum - rightSum);
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const n = parseInt(readLine().trim(), 10);
let arr = Array(n);
for (let i = 0; i < n; i++) {
arr[i] = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
}
const result = diagonalDifference(arr);
ws.write(result + '\n');
ws.end();
}
References.
'HackerRank > Algorithm' 카테고리의 다른 글
[HackerRank/Algorithms] Warmup - Birthday Cake Candles (0) | 2022.08.22 |
---|---|
[HackerRank/Algorithms] Warmup - Mini-Max Sum (0) | 2022.08.22 |
[HackerRank/Algorithms] Warmup - Plus Minus (0) | 2022.08.21 |
[HackerRank/Algorithms] Warmup - A Very Big Sum (0) | 2022.08.19 |
[HackerRank/Algorithms] Warmup - Solve Me First (0) | 2022.08.18 |
Comments