HackerRank > big-sorting

2025. 3. 22. 22:51·🔒Algorithm

➕문제링크

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
'🔒Algorithm' 카테고리의 다른 글
  • LeetCode [JS] > 392. Is Subsequence
  • LeetCode [JS] > Third Maximum Number
  • LeetCode[JS] > 2718. Sum of Matrix After Queries
  • LeetCode [JS] > 3192. Minimum Operations to Make Binary Array Elements Equal to One II
devWarrior
devWarrior
  • devWarrior
    devWarrior
    devWarrior
  • 전체
    오늘
    어제
    • 🧩Dev (263)
      • ⭐FE (34)
      • 🔒Algorithm (155)
      • ➕Etc. (11)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 글쓰기
    • 관리
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    javascript
    오블완
    nodejs
    티스토리챌린지
    BFS
    실버2
    실버1
    Easy
    코딩테스트
    js
    실버4
    구현
    자스
    leetcode
    프론트엔드
    react
    코테
    자바스크립트
    그리디
    백준
    Lv2
    프로그래머스
    dp
    FE
    골드5
    알고리즘
    node.js
    DFS
    실버3
    Algorithm
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
devWarrior
HackerRank > big-sorting
상단으로

티스토리툴바