ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฌธ์ œ

๋ฌธ์ œ ์„ค๋ช…

0 ๋˜๋Š” ์–‘์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ •์ˆ˜๋ฅผ ์ด์–ด ๋ถ™์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ์•Œ์•„๋‚ด ์ฃผ์„ธ์š”.

์˜ˆ๋ฅผ ๋“ค์–ด, ์ฃผ์–ด์ง„ ์ •์ˆ˜๊ฐ€ [6, 10, 2]๋ผ๋ฉด [6102, 6210, 1062, 1026, 2610, 2106]๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ์ด์ค‘ ๊ฐ€์žฅ ํฐ ์ˆ˜๋Š” 6210์ž…๋‹ˆ๋‹ค.

0 ๋˜๋Š” ์–‘์˜ ์ •์ˆ˜๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด numbers๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ˆœ์„œ๋ฅผ ์žฌ๋ฐฐ์น˜ํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ์–ด return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ
  • numbers์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 100,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • numbers์˜ ์›์†Œ๋Š” 0 ์ด์ƒ 1,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ์ •๋‹ต์ด ๋„ˆ๋ฌด ํด ์ˆ˜ ์žˆ์œผ๋‹ˆ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ์–ด return ํ•ฉ๋‹ˆ๋‹ค.
์ž…์ถœ๋ ฅ ์˜ˆnumbersreturn
[6, 10, 2] "6210"
[3, 30, 34, 5, 9] "9534330"

 

์ฝ”๋“œ

function solution(numbers) {
  var answer = "";
  let max = Number.MIN_SAFE_INTEGER;
  let n = numbers.length;
  let ch = Array.from({ length: n }, () => 0);
  let tmp = [];
  
  function DFS(L) {
    if (L === n) {
      max = Math.max(max, Number(tmp.join('')));
      return;
    } else {
      for (let i = 0; i < n; i++) {
        if (ch[i] === 0) {
          ch[i] = 1;
          tmp.push(numbers[i]);
          DFS(L + 1);
          tmp.pop(numbers[i]);
          ch[i] = 0;
        }
      }
    }
  }

  DFS(0);
  answer = String(max);
  return answer;
}

 

์˜ค๋‹ต

์˜ˆ์‹œ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ๋‹ค ๋งž์•˜์ง€๋งŒ ์‹ค์ œ ํ…Œ์ŠคํŠธ๋ฅผ ๋Œ๋ ธ์„ ๊ฒฝ์šฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ๋กœ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค.์ด์— ํ•ด๋‹น ๊ฐ’๋“ค์„ ๋ฌธ์ž๋กœ ์น˜ํ™˜ํ•ด ์ค€ ๋’ค์— + ์—ฐ๊ฒฐ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋‘ ์กฐํ•ฉ์„ ๋งŒ๋“ค๊ณ  sort() ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ํ•ด๋‹น ๊ฐ’๋“ค์„ ๋น„๊ตํ•˜์—ฌ ๋‚ด๋ฆผ์ฐจ์ˆœํ•ด์ฃผ์—ˆ๋‹ค.

 

์ฝ”๋“œ

function solution(numbers) {
    const answer = numbers.map((number) => number.toString()).sort((a, b) => (b+a) - (a+b)).join("") 
    return answer.replace(/^0+/, "0"); // 1๊ฐœ ์ด์ƒ์˜ ์˜๋ฏธ ์—†๋Š” 0์„ ํ•˜๋‚˜๋กœ ์ค„์˜€๋‹ค.
}
๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ