➕문제링크
https://www.hackerrank.com/challenges/big-sorting/problem?isFullScreen=true
💡문제풀이
문자의 길이가 다를경우 각 자릿수를 비교하는 형태로 수의 대소를 비교하면서 정렬했다.
"use strict";
const fs = require("fs");
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let inputString = "";
let currentLine = 0;
process.stdin.on("data", function (inputStdin) {
inputString += inputStdin;
});
process.stdin.on("end", function () {
inputString = inputString.split("\n");
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'bigSorting' function below.
*
* The function is expected to return a STRING_ARRAY.
* The function accepts STRING_ARRAY unsorted as parameter.
*/
function bigSorting(unsorted) {
// Write your code here
return unsorted.sort((a, b) => {
if (a.length !== b.length) {
return a.length - b.length;
} else {
for (let i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) {
return a[i] - b[i];
}
}
}
});
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const n = parseInt(readLine().trim(), 10);
let unsorted = [];
for (let i = 0; i < n; i++) {
const unsortedItem = readLine();
unsorted.push(unsortedItem);
}
const result = bigSorting(unsorted);
ws.write(result.join("\n") + "\n");
ws.end();
}
'🔒Algorithm' 카테고리의 다른 글
LeetCode [JS] > 392. Is Subsequence (0) | 2025.03.27 |
---|---|
LeetCode [JS] > Third Maximum Number (0) | 2025.03.26 |
LeetCode[JS] > 2718. Sum of Matrix After Queries (0) | 2025.03.15 |
LeetCode [JS] > 3192. Minimum Operations to Make Binary Array Elements Equal to One II (0) | 2025.03.12 |
LeetCode[JS] 53번 Maximum Subarray (0) | 2025.03.11 |