728x90

😢 11650문제를 풀었다면, 보너스 문제
😊 11650문제에서 정렬의 주체만 변경해주면 된다.
y증가를 기준으로 정렬해주는데, y가 같으면 x증가 순으로 정렬해주면 된다.
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 coordinates 2 | |
// For submit | |
// const fs = require('fs'); | |
// const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n'); | |
// For local test | |
const input = ['5', '0 4', '1 2', '1 -1', '2 2', '3 3']; | |
const N = input.shift(); | |
const coordsArr = []; | |
for (let i = 0; i < N; i++) { | |
coordsArr.push(input[i].split(' ').map(strNum => parseInt(strNum))); | |
} | |
let results = ''; | |
coordsArr | |
.sort((a, b) => { | |
if (a[1] !== b[1]) { | |
return a[1] - b[1]; | |
} | |
return a[0] - b[0]; | |
}) | |
.forEach(coords => (results += `${coords[0]} ${coords[1]}\n`)); | |
console.log(results); |
'Algorithm > JavaScript(Node.js)' 카테고리의 다른 글
백준 1181번: 단어 정렬 Node.js(JavaScript) (0) | 2020.02.20 |
---|---|
백준 2751번: 수 정렬하기 2 Node.js(JavaScript) (0) | 2020.02.19 |
백준 11650번: 좌표 정렬하기 Node.js(JavaScript) (0) | 2020.02.18 |
백준 1427번: 소트인사이드 Node.js(JavaScript) (0) | 2020.02.17 |
백준 2108번: 통계학 Node.js(JavaScript) (0) | 2020.02.16 |