728x90

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

😢 시간 초과 문제가 걸릴 줄 알았지만, 간단했던 문제

😊
1. sort() 내림차순 정렬
2. sort() 오름차순 정렬 + reverse()
3. Selection Sort(선택 정렬)
4. Bubble Sort(버블 정렬)
5. Merge Sort(병합 정렬)

위 5가지 방법으로 모두 풀어보았다.
모두 해결 가능했지만, 당연히 시간 차이가 있다.
버블 > 선택 > 병합 === sort() === sort() + reverse() 순으로 시간이 걸렸다.
(버블이 가장 오래걸렸다는 뜻)

// Sort inside
// 1st Solution ( sort() )
// For submit
// const fs = require('fs');
// const input = fs.readFileSync('/dev/stdin').toString().trim().split('').map(num => parseInt(num));
// For local test
const input = [2, 1, 4, 3];
console.log(input.sort((a, b) => b - a).join(''));
// 2nd Solution ( sort(), reverse() )
// For submit
// const fs = require('fs');
// const input = fs.readFileSync('/dev/stdin').toString().trim().split('').map(num => parseInt(num));
// For local test
// const input = [2, 1, 4, 3];
// console.log(input.sort().reverse().join(''));
// 3rd Solution ( Selection Sort )
// For submit
// const fs = require('fs');
// const input = fs.readFileSync('/dev/stdin').toString().trim().split('').map(num => parseInt(num));
// For local test
// let input = [2, 1, 4, 3];
// for (let i = 0; i < input.length; i++) {
// let indexOfMax = i;
// for (let j = i + 1; j < input.length; j++) {
// if (input[indexOfMax] < input[j]) {
// indexOfMax = j;
// }
// }
// if (indexOfMax !== i) {
// const maxier = input[indexOfMax];
// input[indexOfMax] = input[i];
// input[i] = maxier;
// }
// }
// console.log(input.join(''));
// 4th Solution ( Bubble Sort )
// For submit
// const fs = require('fs');
// const input = fs.readFileSync('/dev/stdin').toString().trim().split('').map(num => parseInt(num));
// For local test
// let input = [2, 1, 4, 3];
// for (let i = 0; i < input.length; i++) {
// for (let j = 0; j < input.length - 1 - i; j++) {
// if (input[j] < input[j + 1]) {
// const maxier = input[j + 1];
// input[j + 1] = input[j];
// input[j] = maxier;
// }
// }
// }
// console.log(input.join(''));
// 5th Solution( Merge Sort )
// For submit
// const fs = require('fs');
// const input = fs.readFileSync('/dev/stdin').toString().trim().split('').map(num => parseInt(num));
// For local test
// let input = [2, 1, 4, 3];
// function mergeSort(arr) {
// if (arr.length === 1) {
// return arr;
// }
// const center = Math.floor(arr.length / 2);
// const left = arr.slice(0, center);
// const right = arr.slice(center);
// return merge(mergeSort(left), mergeSort(right));
// }
// function merge(left, right) {
// const results = [];
// while (left.length && right.length) {
// if (left[0] > right[0]) {
// results.push(left.shift());
// } else {
// results.push(right.shift());
// }
// }
// return [...results, ...left, ...right];
// }
// console.log(mergeSort(input).join(''));
view raw bj_js1427.js hosted with ❤ by GitHub

+ Recent posts