Notice
Recent Posts
Recent Comments
Link
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[๋ฐฑ์ค€ 2108๋ฒˆ] ํ†ต๊ณ„ํ•™ - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Algorithm Retrospect

[๋ฐฑ์ค€ 2108๋ฒˆ] ํ†ต๊ณ„ํ•™ - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs)

Lennon 2022. 1. 26. 00:42
728x90
๋ฐ˜์‘ํ˜•

https://www.acmicpc.net/problem/2108

 

2108๋ฒˆ: ํ†ต๊ณ„ํ•™

์ฒซ์งธ ์ค„์— ์ˆ˜์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 500,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹จ, N์€ ํ™€์ˆ˜์ด๋‹ค. ๊ทธ ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ์ •์ˆ˜๋“ค์ด ์ฃผ์–ด์ง„๋‹ค. ์ž…๋ ฅ๋˜๋Š” ์ •์ˆ˜์˜ ์ ˆ๋Œ“๊ฐ’์€ 4,000์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

www.acmicpc.net

const fs = require('fs');
let input = fs.readFileSync('dev/stdin').toString().trim().split('\n');
input.shift();
input = input.map((v) => +v).sort((a, b) => a - b);

// ํ‰๊ท 
console.log(Math.round(input.reduce((prev, cur) => prev + cur, 0) / input.length));
// ์ค‘์•™๊ฐ’
console.log(input[(input.length - 1) / 2]);

let obj = {};

for (let i = 0; i < input.length; i++) {
  if (obj.hasOwnProperty(String(input[i]))) {
    obj[String(input[i])] += 1;
  } else {
    obj[String(input[i])] = 1;
  }
}

obj = Object.entries(obj).sort(([a, b], [c, d]) => {
  if (b === d) {
    return a - c;
  } else {
    return d - b;
  }
});
if (obj.length === 1) {
  console.log(obj[0][0]);
} else if (obj[0][1] === obj[1][1]) {
  console.log(obj[1][0]);
} else {
  console.log(obj[0][0]);
}
// ์ตœ๋นˆ๊ฐ’

// ๋ฒ”์œ„
console.log(Math.abs(Math.max(...input) - Math.min(...input)));

์ตœ๋นˆ๊ฐ’์€ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด hasOwnProperty๋ฅผ ์ด์šฉํ•ด ๊ฐœ์ˆ˜๋ฅผ ์‰ฝ๊ฒŒ ๊ตฌํ•ด์ฃผ๊ณ , ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ๋ฐฐ์—ด๋กœ ๋‹ค์‹œ ๋ณ€ํ™˜ ํ›„ ์ •๋ ฌํ•ด์ค€๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•

'๐Ÿ”ฅ Algorithm > Algorithm Retrospect' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

๋ฐฐ์—ด์—์„œ๋Š” join("").substring() ๋Œ€์‹  slice().join("")์„  (0) 2021.11.05
Comments