- Published on
 
프로그래머스 가장큰수
- Authors
 
- Name
 - Deokgoo Kim
 
문제 풀이
정렬 문제입니다 난이도는 개인적으로 중상이라고 생각합니다.
 처음 접근할때 잘못 접근해서 시간이 좀 걸렸습니다.
 정답 알고리즘 까지 생각하는데 시간이 걸렸습니다.
 자릿수를 4자리 까지 채워서 비교하여 풀면 정답입니다.
 ex) 12 => 1212, 3 => 3333, 421 => 4214
function solution(numbers) {
  const answer = numbers.sort((a, b) => {
    if(a === b) return 0;
    let strA = a.toString();
    let strB = b.toString();
    if(strA.length !== 4) {
      strA+=(strA+strA+strA);
    }
    if(strB.length !== 4) {
      strB+=(strB+strB+strB);
    }
    strA = strA.substr(0, 4);
    strB = strB.substr(0, 4);
    return parseInt(strA) > parseInt(strB) ? -1:1;
  });
  return answer[0]?answer.join(''):'0';
}