new Set을 이용해 중복을 제거할 줄만 알면 좀 할만한 알고리즘 문제
compare 함수 짜는거 은근 머리아프다
// readline 모듈보다는 fs를 이용해 파일 전체를 읽어 들여 처리하기
const fs = require('fs')
const getInput = (filePath) => {
return fs
.readFileSync(filePath)
.toString()
.trim()
.split(/\n/);
}
const input = getInput('/dev/stdin'); // 제출시 여기 '/dev/stdin' 으로 변경
let count = Number(input[0]);
let data = new Array();
for(let i = 1; i <= count ; i++){
data.push(input[i]);
}
data = [...new Set(data)]; // 중복제거
function compare(a,b)
{
if(a.length != b.length)
{
return a.length - b.length;
}
else // 길이가 같으면 사전 순으로
{
if(a>b) return 1;
else if(a<b) return -1;
else return 0;
}
}
data.sort(compare);
for (let x of data)
{
console.log(x);
}'알고리즘 > 백준' 카테고리의 다른 글
| [백준] js 10814 - compare 함수 까먹지 말기 (0) | 2024.02.12 |
|---|---|
| [백준] js 18870 (0) | 2024.02.12 |
| [백준] js 11651 (0) | 2024.02.12 |
| [백준] js 11650 (0) | 2024.02.12 |
| [백준] js 11004 (1) | 2024.02.09 |