๐Ÿ“Œ 1. ๋ฌธ์ œ


Sorting. Bubble sort


๋ฒ„๋ธ” ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•˜๊ณ ,countํ•˜๊ธฐ


3 2 1


โœ ํ’€์ด

function countSwaps(a) {
  let cnt = 0;
  let answer = a;
  for (let i = 0; i < a.length - 1; i++) {
    for (let j = 0; j < a.length - i - 1; j++) {
      if (a[j] > a[j + 1]) {
        cnt++;
        [a[j], a[j + 1]] = [a[j + 1], a[j]];
      }
    }
  }
  console.log(`Array is sorted in ${cnt} swaps.`);
  console.log(`First Element: ${answer[0]}`);
  console.log(`Last Element: ${answer[answer.length - 1]}`);
}


๐Ÿ“Œ 2. ๋ฌธ์ œ


์ •๋ ฌ. ๊ฐ€์žฅ ํฐ ์ˆ˜


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


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


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


numbers return
[6, 10, 2] โ€œ6210โ€


โœ ํ’€์ด

function solution(numbers) {
  let arr = numbers.map((num) => num.toString());
  arr.sort((a, b) => b + a - (a + b)).join("");
  return arr[0] === "0" ? "0" : arr.join("");
}