삽입 정렬과 거품 정렬을 이용해서 풀었음
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// 삽입 정렬
const insertionSort = array => {
for (let i = 1; i < array.length; i++) {
let prev = i - 1;
let current = array[i];
while (prev >= 0 && current < array[prev]) {
array[prev + 1] = array[prev];
prev--;
}
array[prev + 1] = current;
}
return array;
};
// 거품 정렬
const bubbleSort = array => {
for (let i = 0; i < array.length; i++) {
for (let j = 1; j < array.length - i; j++) {
if (array[j] < array[j - 1]) {
const temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
return array;
};
const input = [];
rl.on("line", line => {
input.push(+line);
if (input.length >= 2 && input[0] === input.length - 1) rl.close();
}).on("close", () => {
input.shift();
//정답 기록
let answer = "";
bubbleSort(input).forEach(v => (answer += `${v}\n`));
console.log(answer);
process.exit();
});
삽입 정렬, 퀵 정렬, 힙 정렬로 풀어봤는데 각각 메모리 초과, 시간 초과, 런타임 에러(stackSizeExceedd)에러가 나서 JS의 내장함수인 array.prototype.sort()
를 이용해서 풀었음
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const input = [];
rl.on("line", line => {
input.push(+line);
if (input.length >= 2 && input[0] === input.length - 1) rl.close();
}).on("close", () => {
input.shift();
const array = input;
//정답 기록
let answer = "";
array.sort((x, y) => (x === y ? 0 : x > y ? 1 : -1));
array.forEach(v => (answer += `${v}\n`));
console.log(answer);
process.exit();
});
카운팅 정렬을 사용했으나 메모리 초과 발생
/**
* 카운팅 정렬 사용
*/
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const input = [];
rl.on("line", line => {
input.push(+line);
if (input.length >= 2 && input[0] === input.length - 1) rl.close();
}).on("close", () => {
input.shift();
//정답 기록
let answer = "";
const maxNumber = Math.max(...input);
const array = Array(maxNumber).fill(0);
input.forEach(v => array[v - 1]++);
array.forEach((v, index) => {
for (let i = 0; i < v; i++) {
answer += `${index + 1}\n`;
}
});
console.log(answer);
process.exit();
});
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const input = [];
rl.on("line", line => {
input.push(+line);
if (input.length >= 2 && input[0] === input.length - 1) rl.close();
}).on("close", () => {
input.shift();
const array = input;
array.sort((x, y) => (x === y ? 0 : x > y ? 1 : -1));
// 음수값도 존재하기 때문에 객체에다가 넣음 ( 프로퍼티키: 입력숫자, 프로퍼티밸류: 반복수 )
const obj = {};
array.forEach(v => {
if (obj[v]) {
obj[v]++;
} else {
obj[v] = 1;
}
});
let maximumRepeat = 0;
const tempArray = [];
// 반복수가 제일 많은 반복수를 찾음
for (let p in obj) {
if (obj[p] > maximumRepeat) {
maximumRepeat = obj[p];
}
}
// 반복수가 제일 많은 놈들을 tempArray에 넣음
for (let p in obj) {
if (obj[p] === maximumRepeat) {
tempArray.push(+p);
}
}
// 오름차순 정렬
tempArray.sort((x, y) => (x > y ? 1 : -1));
let mean = Math.round(array.reduce((prev, curr) => prev + curr) / array.length);
let median = array[Math.floor(array.length / 2)];
let mode = tempArray.length >= 2 ? tempArray[1] : tempArray[0];
let range = array[array.length - 1] - array[0];
let answer = "";
answer = `${mean}\n${median}\n${mode}\n${range}\n`;
console.log(answer);
process.exit();
});
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const input = [];
rl.on("line", line => {
input.push(line);
rl.close();
}).on("close", () => {
const numberArray = input.shift().split("");
numberArray.sort((x, y) => (x === y ? 0 : x < y ? 1 : -1));
let answer = numberArray.join("");
console.log(answer);
process.exit();
});
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const input = [];
rl.on("line", line => {
input.push(line);
if (input.length >= 2 && +input[0] === input.length - 1) rl.close();
}).on("close", () => {
input.shift();
const array = input.map(v => v.split(" ").map(v => +v));
array.sort((x, y) => {
if (x[0] === y[0]) {
if (x[1] > y[1]) return 1;
else return -1;
} else if (x[0] > y[0]) {
return 1;
} else {
return -1;
}
});
let answer = "";
array.forEach(v => (answer += `${v.join(" ")}\n`));
console.log(answer);
process.exit();
});
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const input = [];
rl.on("line", line => {
input.push(line);
if (input.length >= 2 && +input[0] === input.length - 1) rl.close();
}).on("close", () => {
input.shift();
const array = input.map(v => v.split(" ").map(v => +v));
array.sort((x, y) => {
if (x[1] === y[1]) {
if (x[0] > y[0]) return 1;
else return -1;
} else if (x[1] > y[1]) {
return 1;
} else {
return -1;
}
});
let answer = "";
array.forEach(v => (answer += `${v.join(" ")}\n`));
console.log(answer);
process.exit();
});
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const input = [];
rl.on("line", line => {
input.push(line);
if (input.length >= 2 && +input[0] === input.length - 1) rl.close();
}).on("close", () => {
input.shift();
// 중복제거
const array = [...new Set(input)];
const tempArray = [];
array.forEach(v => {
if (tempArray[v.length] === undefined) tempArray[v.length] = [];
tempArray[v.length].push(v);
});
let answer = "";
tempArray.forEach(value => {
if (value.length === 1) return (answer += `${value[0]}\n`);
value.sort((x, y) => (x > y ? 1 : -1));
value.forEach(v => (answer += `${v}\n`));
});
console.log(answer);
process.exit();
});
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const input = [];
rl.on("line", line => {
input.push(line);
if (input.length >= 2 && +input[0] === input.length - 1) rl.close();
}).on("close", () => {
input.shift();
const array = input.map((v, i) => {
const tempArray = v.split(" ").map((v, i) => (i === 0 ? +v : v));
tempArray.push(i);
return tempArray;
});
array.sort((x, y) => {
if (x[0] === y[0]) {
return x[2] > y[2] ? 1 : -1;
} else {
return x[0] > y[0] ? 1 : -1;
}
});
let answer = "";
array.forEach(v => (answer += `${v[0]} ${v[1]}\n`));
console.log(answer);
process.exit();
});
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const input = [];
rl.on("line", line => {
input.push(line);
if (input.length >= 2 && +input[0] === input[1].split(" ").length) rl.close();
}).on("close", () => {
input.shift();
// 문자열 => 배열 => 숫자배열 => 중복제거
const array = [...new Set(input[0].split(" ").map(v => +v))];
array.sort((x, y) => (x > y ? 1 : -1));
const table = {};
array.forEach((v, i) => (table[v] = i));
let answer = "";
input[0].split(" ").forEach(v => (answer += `${table[v]} `));
console.log(answer);
process.exit();
});