2108. 통계학 - node.js / javascript
문제
내 코드
let fs = require("fs");
let input = fs
.readFileSync("/dev/stdin")
.toString()
.trim()
.split("\n")
.map((num) => parseInt(num));
const N = Number(input.shift());
const sortedNumArr = input.sort((a, b) => Number(a) - Number(b));
const numMap = {};
for (let num of sortedNumArr) {
if (numMap[num]) {
numMap[num] += 1;
} else {
numMap[num] = 1;
}
}
let hitMaxNum = Math.max.apply(null, Object.values(numMap));
let hitMaxNumArr = [];
let hitMaxNumResult = 0;
for (let numKey in numMap) {
if (numMap[numKey] === hitMaxNum) {
hitMaxNumArr.push(numKey);
}
}
if (hitMaxNumArr.length > 1) {
hitMaxNumArr = hitMaxNumArr.sort((a, b) => a - b);
hitMaxNumResult = hitMaxNumArr[1];
} else {
hitMaxNumResult = hitMaxNumArr[0];
}
const avg = Math.round(input.reduce((acc, num) => (acc += num), 0) / N);
const center = input[Math.floor(input.length / 2)];
const maxSubMin = sortedNumArr[sortedNumArr.length - 1] - sortedNumArr[0];
console.log(avg);
console.log(center);
console.log(hitMaxNumResult);
console.log(maxSubMin);
깃허브 링크
let fs = require("fs");
let input = fs
.readFileSync("/dev/stdin")
.toString()
.trim()
.split("\n")
.map((num) => parseInt(num));
const N = Number(input.shift());
const sortedNumArr = input.sort((a, b) => Number(a) - Number(b));
const numMap = {};
for (let num of sortedNumArr) {
if (numMap[num]) {
numMap[num] += 1;
} else {
numMap[num] = 1;
}
}
let hitMaxNum = Math.max.apply(null, Object.values(numMap));
let hitMaxNumArr = [];
let hitMaxNumResult = 0;
for (let numKey in numMap) {
if (numMap[numKey] === hitMaxNum) {
hitMaxNumArr.push(numKey);
}
}
if (hitMaxNumArr.length > 1) {
hitMaxNumArr = hitMaxNumArr.sort((a, b) => a - b);
hitMaxNumResult = hitMaxNumArr[1];
} else {
hitMaxNumResult = hitMaxNumArr[0];
}
const avg = Math.round(input.reduce((acc, num) => (acc += num), 0) / N);
const center = input[Math.floor(input.length / 2)];
const maxSubMin = sortedNumArr[sortedNumArr.length - 1] - sortedNumArr[0];
console.log(avg);
console.log(center);
console.log(hitMaxNumResult);
console.log(maxSubMin);
https://github.com/highjoon/JS_Algorithm/blob/master/BOJ/%EC%A0%95%EB%A0%AC/2108.js
Author And Source
이 문제에 관하여(2108. 통계학 - node.js / javascript), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@arthur/2108.-통계학-node.js-javascript저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)