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

Lennon FE

[๋ฐฑ์ค€ 18870๋ฒˆ] ์ขŒํ‘œ ์••์ถ• - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Baekjoon

[๋ฐฑ์ค€ 18870๋ฒˆ] ์ขŒํ‘œ ์••์ถ• - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs)

Lennon 2022. 1. 19. 23:27
728x90
๋ฐ˜์‘ํ˜•

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

 

18870๋ฒˆ: ์ขŒํ‘œ ์••์ถ•

์ˆ˜์ง์„  ์œ„์— N๊ฐœ์˜ ์ขŒํ‘œ X1, X2, ..., XN์ด ์žˆ๋‹ค. ์ด ์ขŒํ‘œ์— ์ขŒํ‘œ ์••์ถ•์„ ์ ์šฉํ•˜๋ ค๊ณ  ํ•œ๋‹ค. Xi๋ฅผ ์ขŒํ‘œ ์••์ถ•ํ•œ ๊ฒฐ๊ณผ X'i์˜ ๊ฐ’์€ Xi > Xj๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ์ขŒํ‘œ์˜ ๊ฐœ์ˆ˜์™€ ๊ฐ™์•„์•ผ ํ•œ๋‹ค. X1, X2, ..., XN์— ์ขŒ

www.acmicpc.net

 

const fs = require('fs');
let input = fs.readFileSync('dev/stdin').toString().trim().split('\n');
input.shift();
input = input[0].split(' ');

let inputArr = [...input];
inputArr = [...new Set(inputArr)].sort((a, b) => a - b);

const arr = [];
const obj = {};

inputArr.forEach((v, idx) => (obj[v] = idx));

for (let i = 0; i < input.length; i++) {
  arr.push(obj[input[i]]);
}

console.log(arr.join(' '));

input์˜ ์ตœ๋Œ€๊ฐ’์ด 1,000,000์ด๋ฏ€๋กœ ์•ˆ ๊ทธ๋ž˜๋„ ๋Š๋ฆฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ํ™œ์šฉํ•ด์•ผ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

 

์šฐ์„  ๊ทธ๋ƒฅ for๋ฌธ์œผ๋กœ ํ’€๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚œ๋‹ค.

 

์ขŒํ‘œ ์••์ถ• ๊ฐ’์„ ์–ป๊ธฐ ์œ„ํ•ด Set์„ ์ด์šฉํ•ด ์ค‘๋ณต๋œ ๊ฐ’์„ ์ œ๊ฑฐํ•˜๊ณ , ์ž‘์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜์˜€๋‹ค.

ex) ['-1', '-1', '3', '5', '5', '7']   => ['-1', '3', '5', '7']

 

๊ทธ ํ›„ ์‹œ๊ฐ„์ดˆ๊ณผ ํ•ด๊ฒฐ์„ ์œ„ํ•ด ์œ„์˜ ๊ฐ’์„ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด obj๊ฐ’์œผ๋กœ ๋„ฃ์–ด์คฌ๋‹ค. 

// ex) ์œ„์˜ ๊ฐ’ [-1, 3, 5, 7]

obj = {
 '-1' : 0,
  '3' : 1,
  '5' : 2,
  '7' : 3,
}

๊ทธ ํ›„ input๊ฐ’์„ ๋Œ๋ฆฌ๋ฉฐ ํ•ด๋‹นํ•˜๋Š” ์ธ๋ฑ์Šค ๊ฐ’์„ pushํ•ด์ค€ ํ›„ console๋กœ join์„ ์ด์šฉํ•ด ๋„์šฐ๋ฉด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•
Comments