728x90

😢 시간 초과 문제가 걸릴 줄 알았지만, 간단했던 문제
😊
1. sort() 내림차순 정렬
2. sort() 오름차순 정렬 + reverse()
3. Selection Sort(선택 정렬)
4. Bubble Sort(버블 정렬)
5. Merge Sort(병합 정렬)
위 5가지 방법으로 모두 풀어보았다.
모두 해결 가능했지만, 당연히 시간 차이가 있다.
버블 > 선택 > 병합 === sort() === sort() + reverse() 순으로 시간이 걸렸다.
(버블이 가장 오래걸렸다는 뜻)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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('')); |
'Algorithm > JavaScript(Node.js)' 카테고리의 다른 글
백준 11651번: 좌표 정렬하기 2 Node.js(JavaScript) (0) | 2020.02.18 |
---|---|
백준 11650번: 좌표 정렬하기 Node.js(JavaScript) (0) | 2020.02.18 |
백준 2108번: 통계학 Node.js(JavaScript) (0) | 2020.02.16 |
백준 1436번: 영화감독 숌 Node.js(JavaScript) (0) | 2020.02.14 |
백준 1018번: 체스판 다시 칠하기 Node.js(JavaScript) (0) | 2020.02.13 |