๐ŸŸฃ [์—˜๋ฆฌ์Šค ํ”„๋ฆฌํŠธ๋ž™] ๋ฌธ์ œ ์ •๋ฆฌ

๊น€ํƒœ์šฑยท2023๋…„ 7์›” 31์ผ
0
post-thumbnail

์—˜๋ฆฌ์ŠคํŠธ๋ž™ ํ”„๋ฆฌํŠธ๋ž™ ๊ณผ์ • ๋ฌธ์ œ #1

[์—˜๋ฆฌ์Šค ํ”„๋ฆฌํŠธ๋ž™] : ๊ตฌ์Šฌ ๊พธ๋Ÿฌ๋ฏธ

๋ฌธ์ œ ์„ค๋ช…
์—˜๋ฆฌ์Šค ํ† ๋ผ๋Š” ๊ตฌ์Šฌ ์žฅ์‚ฌ๋ฅผ ์œ„ํ•ด ๊ตฌ์Šฌ์„ ๊พธ๋Ÿฌ๋ฏธ์— ๋‹ด์•„ ํฌ์žฅ์„ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์—˜๋ฆฌ์Šค ํ† ๋ผ๊ฐ€ ์ค€๋น„ํ•œ ๊ตฌ์Šฌ์€ ์ƒ‰์ƒ๋ณ„๋กœ ๋ฌด๊ฒŒ๊ฐ€ ๋ชจ๋‘ ๋‹ค๋ฅด๋ฉฐ ๊ตฌ์Šฌ ๊พธ๋Ÿฌ๋ฏธ ๋˜ํ•œ ๊ตฌ์Šฌ์„ ๋‹ด์•„๋‚ผ ์ˆ˜ ์žˆ๋Š” ๋ฌด๊ฒŒ๊ฐ€ ๋ชจ๋‘ ๋‹ฌ๋ผ ์ตœ์†Œํ•œ์˜ ๊ตฌ์Šฌ ๊ฐœ์ˆ˜๋ฅผ ํ™œ์šฉํ•ด ๊พธ๋Ÿฌ๋ฏธ๋ฅผ ์ฑ„์šฐ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์ƒ‰๊น”๊ณผ ๋ฌด๊ฒŒ๊ฐ€ ๋‹ค๋ฅธ 3๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ๊ตฌ์Šฌ์ด ๋ฌด์ œํ•œ์œผ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

๊ตฌ์Šฌ ๋ฌด๊ฒŒ
๋นจ๊ฐ„ ๊ตฌ์Šฌ 250g
ํŒŒ๋ž€ ๊ตฌ์Šฌ 40g
ํฐ ๊ตฌ์Šฌ 10g

์˜ˆ๋ฅผ ๋“ค์–ด, 300 300g์˜ ๊พธ๋Ÿฌ๋ฏธ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋นจ๊ฐ„ ๊ตฌ์Šฌ 1๊ฐœ, ํŒŒ๋ž€ ๊ตฌ์Šฌ 1๊ฐœ, ํฐ ๊ตฌ์Šฌ
1๊ฐœ๋กœ ์ตœ์†Œ 3๊ฐœ์˜ ๊ตฌ์Šฌ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ์‚ฌํ•ญ

  • input ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ๋ถ€ํ„ฐ ๊ตฌ์Šฌ ๊พธ๋Ÿฌ๋ฏธ์˜ ๋ฌด๊ฒŒ๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  ๊พธ๋Ÿฌ๋ฏธ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ตœ์†Œ ๊ตฌ์Šฌ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.
    (1โ‰คinputโ‰ค10,000)

  • ๋งŒ์•ฝ ๋ฌด๊ฒŒ์— ๋งž์ถ”์–ด ๊พธ๋Ÿฌ๋ฏธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•˜์„ธ์š”.

์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ์ถœ๋ ฅ
3003
5504
65-1

๋‚˜์˜ ํ’€์ด

// ์ง€์‹œ์‚ฌํ•ญ์„ ์ฐธ๊ณ ํ•˜์—ฌ solution ํ•จ์ˆ˜ ์•ˆ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.
function solution(input) {
  let bead = [
    { color: 'redBead', weight: 250 },
    { color: 'whiteBead', weight: 40 },
    { color: 'blueBead', weight: 10 },
  ];
  let count = 0;

  for (let i = 0; i < bead.length; i++) {
    let needBead = bead[i];
    while (input >= needBead.weight) {
      input -= needBead.weight;
      count += 1;
    }
  }
  if (input !== 0) count = -1;

  return count;
}

ํ’€์ด ์ ‘๊ทผ

  1. ๊ฐ ๊ตฌ์Šฌ ๋ณ„ ๋ชฉ๋ก๊ณผ ๋ฌด๊ฒŒ์˜ ๊ฐ์ฒด๋ฅผ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ฌ

  2. ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ์ฒด ์† ๋ชฉ๋ก์„ ๊บผ๋ƒ„

  3. ๋ฐ˜๋ณต ์•ˆ์— while์„ ์ค‘๋ณตํ•˜์—ฌ ๊ฐ ๊ตฌ์Šฌ ๋ฌด๊ฒŒ๊ฐ€ input๋งŒํผ ํ•„์š”ํ•œ ์ตœ์†Œ ๋ฌด๊ฒŒ๋ฅผ ๊ตฌํ•  ๋•Œ๊นŒ์ง€ ์ˆœํšŒ

  4. input์— ํ•„์š”ํ•œ ๋ฌด๊ฒŒ ๊ฐ’๋งŒํผ count๋ฅผ ๋”ํ•ด์ค˜ ๊ฐ ๋ฌด๊ฒŒ๋ณ„๋กœ ๋ช‡ ๊ฐœ์”ฉ ํ•„์š”ํ•œ์ง€ ๊ตฌํ•จ


์—˜๋ฆฌ์ŠคํŠธ๋ž™ ํ”„๋ฆฌํŠธ๋ž™ ๊ณผ์ • ๋ฌธ์ œ #2

[์—˜๋ฆฌ์Šค ํ”„๋ฆฌํŠธ๋ž™] : ๋งˆ์ฒœ๋ฃจ

๋ฌธ์ œ ์„ค๋ช…
์ฝ”๋”๋žœ๋“œ์˜ ์œ ๋Šฅํ•œ ๊ฑด์ถ•๊ฐ€ ์—˜๋ฆฌ์Šค ํ† ๋ผ๋Š” ๋ชจ์ž์žฅ์ˆ˜๋กœ๋ถ€ํ„ฐ ์ƒˆ๋กœ์šด ์‚ฌ์—…์„ ์ œ์•ˆ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค.

๋ฐ”๋กœ ์ฝ”๋”๋žœ๋“œ ํ•œ๊ฐ€์šด๋ฐ ๋งˆ์ฒœ๋ฃจ๋ฅผ ์ง€์–ด ๊ด€๊ด‘๊ฐ์„ ์œ ์น˜ํ•˜๋Š” ์‚ฌ์—…์ด์˜€์Šต๋‹ˆ๋‹ค.

์ง€์‹œ์‚ฌํ•ญ์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • ๋งˆ์ฒœ๋ฃจ์˜ ๋†’์ด๋ฅผ input ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์•„ ์•„๋ž˜์˜ ์กฐ๊ฑด์„ ์ฐธ๊ณ ํ•˜์—ฌ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋งŒํผ์˜ ๋†’์ด๋ฅผ ๊ฐ€์ง€๋Š” ๋งˆ์ฒœ๋ฃจ๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.

  • ์ถœ๋ ฅํ•  ๊ฐ’์€ solution ํ•จ์ˆ˜ ์•ˆ์—์„œ return ํ•ด์ฃผ์„ธ์š”.

์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ์ถœ๋ ฅ
3*
-**
--***

๋‚˜์˜ ํ’€์ด

// ์ง€์‹œ์‚ฌํ•ญ์„ ์ฐธ๊ณ ํ•˜์—ฌ solution ํ•จ์ˆ˜ ์•ˆ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.
function solution(input) {
  let star = '';
  for (let i = 1; i <= input; i++) {
    for (let j = 0; j < i; j++) {
      if (j >= 5) continue;
      star += '*';
    }
    if (i !== input) {
      // ๋งˆ์ง€๋ง‰ ๋ฐ˜๋ณต์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋งŒ ๊ฐœํ–‰ ๋ฌธ์ž ์ถ”๊ฐ€
      star += '\n';
    }
  }
  return star;
}

ํ’€์ด ์ ‘๊ทผ

  1. ๋ฐ˜๋ณตํ•ด์„œ ๋ณ„์„ ์ฐ์–ด๋‚ด๋Š” ์ฝ”๋“œ์™€ ๋˜‘๊ฐ™์ง€๋งŒ ๊ธธ์ด๊ฐ€ 5 ์ด์ƒ๋˜๋ฉด
  2. ๋”์ด์ƒ ๋ณ„์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๊ณ  ๊ธธ์ด 5๋งŒํผ์˜ ๋ณ„์„ ๋˜‘๊ฐ™์ด ์Œ“์•„๊ฐ€๋ฉด ๋จ.
  3. ๋งˆ์ง€๋ง‰ ๋ฐ˜๋ณต์—์„œ ๊ฐœํ–‰์ด ์ถ”๊ฐ€ ๋์—ˆ์–ด์„œ ์กฐ๊ฑด์„ ํ†ตํ•ด ๋งˆ์ง€๋ง‰ ๋ฐ˜๋ณต์—๋Š” ๊ฐœํ–‰์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๊ธฐ๋กœ ํ•จ.

์—˜๋ฆฌ์ŠคํŠธ๋ž™ ํ”„๋ฆฌํŠธ๋ž™ ๊ณผ์ • ๋ฌธ์ œ #3

[์—˜๋ฆฌ์Šค ํ”„๋ฆฌํŠธ๋ž™] : ๋ฐ˜์ชฝ์งœ๋ฆฌ ํ”ผ๋ผ๋ฏธ๋“œ

๋ฌธ์ œ ์„ค๋ช…
์—˜๋ฆฌ์Šค ํ† ๋ผ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ˆซ์ž๋งŒํผ ๋†’์ด๋ฅผ ๊ฐ€์ง€๋Š” ๋ฐ˜์ชฝ ํ”ผ๋ผ๋ฏธ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์ง€์‹œ์‚ฌํ•ญ์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ
*num ๋งค๊ฐœ๋ณ€์ˆ˜์—์„œ ์ž์—ฐ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ๋œ ์ˆซ์ž๋งŒํผ ๋†’์ด๋ฅผ ๊ฐ€์ง€๋Š” ๋ฐ˜์ชฝ ํ”ผ๋ผ๋ฏธ๋“œ๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.

์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ์ถœ๋ ฅ
3--*
-**
***

๋‚˜์˜ ํ’€์ด

function solution(num) {
  let result = '';

  for (let i = 1; i <= num; i++) {
    let answer = '';
    let space = '';

    for (let j = 0; j < num - i; j++) {
      space += ' ';
    }

    for (let k = 0; k < i; k++) {
      answer += '*';
    }

    result += space + answer;
    if (i < num) {
      result += '\n';
    }
  }
  return result;
}

ํ’€์ด ์ ‘๊ทผ

  1. ๋ฐ˜์ชฝ ๊ณต๊ฐ„์„ ๋งŒ๋“ค space ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑ.
  2. ๋‘๊ฐœ์˜ ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๋นˆ๊ณต๊ฐ„์„ ์ƒ์„ฑํ•  ๋ฐ˜๋ณต๋ฌธ, ๋ณ„๋“ค์„ ์ฐ์–ด๋‚ด๋Š” ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ํ•ฉ์ณ๋‚ด๋ฉด ๋จ.

์—˜๋ฆฌ์ŠคํŠธ๋ž™ ํ”„๋ฆฌํŠธ๋ž™ ๊ณผ์ • ๋ฌธ์ œ #4

[์—˜๋ฆฌ์Šค ํ”„๋ฆฌํŠธ๋ž™] : ๊ฒน์น˜๋Š” ๊ตฌ๊ฐ„ ์ฐพ๊ธฐ

๋ฌธ์ œ ์„ค๋ช…
์ˆ˜์ง์„  ์ƒ์— A ๊ตฌ๊ฐ„๊ณผ B ๊ตฌ๊ฐ„์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด A ๊ตฌ๊ฐ„์€ 3 ์ด์ƒ 7 ์ดํ•˜์— ํ•ด๋‹นํ•˜๋ฉฐ, B ๊ตฌ๊ฐ„์€ 5 ์ด์ƒ 9 ์ดํ•˜์— ํ•ด๋‹นํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด 5 ์ด์ƒ 7 ์ดํ•˜์˜ ๊ตฌ๊ฐ„์€ A ๊ตฌ๊ฐ„์ด๋ฉด์„œ ๋™์‹œ์— B ๊ตฌ๊ฐ„์ด ๋ฉ๋‹ˆ๋‹ค.

์œ„์™€ ๊ฐ™์ด, ๋‘ ๊ตฌ๊ฐ„์˜ ๋ฒ”์œ„๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ๋‘ ๊ตฌ๊ฐ„์ด ๊ฒน์น˜๋Š” ๋ฒ”์œ„๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.

๋‘ ๊ตฌ๊ฐ„์ด ๊ฒน์น˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋Š” ๋ณ„๋„๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ์‚ฌํ•ญ

  • ๋„ค ์ค„์— ๊ฑธ์ณ 0์ด์ƒ์˜ ์ •์ˆ˜๊ฐ€ ์ค„๋ฐ”๊ฟˆ์„ ํฌํ•จํ•œ ๋ฌธ์ž์—ด ํ˜•ํƒœ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜ input์— ๋ฐ›์•„์ง‘๋‹ˆ๋‹ค.

    	์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ๊ตฌ๊ฐ„ A์˜ ์ตœ์†Ÿ๊ฐ’์ด ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค.
    	๋‘ ๋ฒˆ์งธ ์ค„์—๋Š” ๊ตฌ๊ฐ„ A์˜ ์ตœ๋Œ“๊ฐ’์ด ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค
    	์„ธ ๋ฒˆ์งธ ์ค„์—๋Š” ๊ตฌ๊ฐ„ B์˜ ์ตœ์†Ÿ๊ฐ’์ด ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค
    	๋„ค ๋ฒˆ์งธ ์ค„์—๋Š” ๊ตฌ๊ฐ„ B์˜ ์ตœ๋Œ“๊ฐ’์ด ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค.
    	๊ฐ ๊ตฌ๊ฐ„์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’์€ ํ•ญ์ƒ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.
    	๊ตฌ๊ฐ„ A์™€ B, ๋‘ ๊ตฌ๊ฐ„์— ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์˜ ์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’์„ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ถœ๋ ฅํ•˜์„ธ์š”.
  • ์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’์ด ๋™์ผํ•œ ๊ฒฝ์šฐ ํ•ด๋‹น ๊ตฌ๊ฐ„์€ ๊ฒน์น˜๋Š” ๊ตฌ๊ฐ„์— ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.
    ๋งŒ์•ฝ ๋‘ ๊ตฌ๊ฐ„์ด ๊ฒน์น˜์ง€ ์•Š๋Š”๋‹ค๋ฉด X๋ฅผ ์ถœ๋ ฅํ•ด ์ฃผ์„ธ์š”.

  • ๊ฐ ๊ตฌ๊ฐ„์„ ๋ฐฐ์—ด์— ๋‹ด์•„ solution ํ•จ์ˆ˜ ์•ˆ์—์„œ return ํ•˜์„ธ์š”.

์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ์ถœ๋ ฅ
3
7[5, 7]
5
9
0
10[3, 8]
3
8

๋‚˜์˜ ํ’€์ด

// ์ง€์‹œ์‚ฌํ•ญ์„ ์ฐธ๊ณ ํ•˜์—ฌ solution ํ•จ์ˆ˜ ์•ˆ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.
let splitString = function (word, seperator) {
  let result = [];
  let currentWord = '';

  if (seperator === '') {
    // separator๊ฐ€ ๋นˆ ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ, ๊ฐ ๊ธ€์ž๋ฅผ ๋ฐฐ์—ด์— ๋‹ด์Œ
    for (let i = 0; i < word.length; i++) {
      result.push(word[i]);
    }
  } else {
    for (let i = 0; i < word.length; i++) {
      if (word[i] === seperator) {
        result.push(currentWord);
        currentWord = '';
      } else {
        currentWord += word[i];
      }
    }
    result.push(currentWord);
  }

  return result;
};

let range = function (min, max) {
  let ansewr = [];
  for (min; min <= max; min++) {
    ansewr.push(min);
  }
  return ansewr;
};

function solution(input) {
  let inputSplit = splitString(input, '\n');
  let rangeA = range(+inputSplit[0], +inputSplit[1]);
  let rangeB = range(+inputSplit[2], +inputSplit[3]);

  let overlap = [];

  for (let i = 0; i < rangeA.length; i++) {
    for (let j = 0; j < rangeB.length; j++) {
      if (rangeA[i] === rangeB[j]) overlap.push(rangeA[i]);
    }
  }

  if (overlap.length === 0) return 'X';
  else return [Math.min(...overlap), Math.max(...overlap)];
}

ํ’€์ด ์ ‘๊ทผ

  1. split ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ตฌํ˜„์„ ํ†ตํ•ด '\n' ๊ฐœํ–‰ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐ.
  2. range ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์„œ ๊ฐ input ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ๋ฒ”์œ„์˜ ๋ฐฐ์—ด ๋ฐ˜ํ™˜
  3. ๋ณ€์ˆ˜ rangeA, rangeB ์„ค์ •ํ•˜์—ฌ ๊ฐ ๋ฒ”์œ„์— ๋งž๋Š” ๋ฐฐ์—ด ๋ฐ˜ํ™˜.
  4. ๋ฐ˜๋ณต ์ค‘์ฒฉ์„ ํ†ตํ•ด ๊ฒน์น˜๋Š” ๋ถ€๋ถ„ overlap ๋ฐฐ์—ด์— push
  5. return ๊ฐ’์œผ๋กœ Math ๋ฉ”์†Œ๋“œ๋ฅผ ์ ์šฉ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ• ์ ์šฉ.

์—˜๋ฆฌ์ŠคํŠธ๋ž™ ํ”„๋ฆฌํŠธ๋ž™ ๊ณผ์ • ๋ฌธ์ œ #5

[์—˜๋ฆฌ์Šค ํ”„๋ฆฌํŠธ๋ž™] : 8์€ ํŠน๋ณ„ํ•ด!

๋ฌธ์ œ ์„ค๋ช…
๊ฐ€๋กœ, ์„ธ๋กœ๋กœ ๊ฐ€์šด๋ฐ๋ฅผ ๊ฐˆ๋ผ๋„ ๋ชจ๋‘ ๊ฐ™์€ ๋ชจ์–‘์ธ 8์„ ์ข‹์•„ํ•˜๋Š” ์—˜๋ฆฌ์Šค ํ† ๋ผ๋Š” 1๋ถ€ํ„ฐ 10000๊นŒ์ง€ 8์ด๋ผ๋Š” ์ˆซ์ž๊ฐ€ ๋ช‡ ๋ฒˆ ๋‚˜์˜ค๋Š”์ง€ ์•Œ์•„๋ณด๋ ค๊ณ  ํ•ด์š”!

์ง€์‹œ์‚ฌํ•ญ์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • 1๋ถ€ํ„ฐ 10,000๊นŒ์ง€์˜ ์ˆ˜ ์ค‘ 8์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์–ด ํ•จ์ˆ˜ ์•ˆ์—์„œ ๋ฆฌํ„ดํ•˜์„ธ์š”.

์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ์ถœ๋ ฅ
// ๋ฒ”์œ„๋ฅผ 1~20๋กœ ๊ฐ€์ •ํ–ˆ์„ ๋•Œ 8์˜ ๊ฐœ์ˆ˜// 8, 182

๋‚˜์˜ ํ’€์ด

// ์ง€์‹œ์‚ฌํ•ญ์„ ์ฐธ๊ณ ํ•˜์—ฌ solution ํ•จ์ˆ˜ ์•ˆ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.
function solution() {
  let count = 0;

  for (let i = 1; i < 10000; i++) {
    let currentNum = i;
    while (currentNum > 0) {
      if (currentNum % 10 === 8) count += 1;
      currentNum = Math.floor(currentNum / 10); // currentNum์„ ์—…๋ฐ์ดํŠธ ํ•ด์ค˜์•ผํ•จ.
    }
  }
  return count;
}

ํ’€์ด ์ ‘๊ทผ

  1. ์ œํ•œ์‚ฌํ•ญ์—์„œ 10000๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์•ผ ํ•˜๋Š” for๋ฌธ ์กฐ๊ฑด์„ ์ƒ์„ฑ
  2. while์„ ํ†ตํ•ด ํ˜„์žฌ ๊ฐ’์ด 8์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด 10์„ ๋‚˜๋ˆˆ ๋’ค ๋‚˜๋จธ์ง€๋Š” ๋ฒ„๋ฆฌ๋Š” ํ˜•์‹์œผ๋กœ ๊ณ„์† ์—…๋ฐ์ดํŠธํ•˜์—ฌ 1์˜ ์ž๋ฆฌ์ˆ˜๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต.

์—˜๋ฆฌ์ŠคํŠธ๋ž™ ํ”„๋ฆฌํŠธ๋ž™ ๊ณผ์ • ๋ฌธ์ œ #6

[์—˜๋ฆฌ์Šค ํ”„๋ฆฌํŠธ๋ž™] : ์•”ํ˜ธ๋ฌธ ํ•ด์„ํ•˜๊ธฐ

๋ฌธ์ œ ์„ค๋ช…
์—๋‹ˆ๊ทธ๋งˆ(Enigma, ์ˆ˜์ˆ˜๊ป˜๋ผ)๋Š” ๋…์ผ๊ตฐ์ด ์ „์žฅ์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ ์•”ํ˜ธ ์ƒ์„ฑ ์žฅ์น˜์ž…๋‹ˆ๋‹ค.

๋…์ผ์–ด ์•ŒํŒŒ๋ฒณ ๊ฐ๊ฐ์— ๋Œ€ํ•˜์—ฌ ๋‹ค๋ฅธ ์•ŒํŒŒ๋ฒณ์— ์ž„์˜๋กœ ๋Œ€์‘์‹œํ‚จ ๋‹ค์Œ, ์ด๋ ‡๊ฒŒ ๋Œ€์‘๋œ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ „์‹  ๋ถ€ํ˜ธ๋ฅผ ๋ฐœ์†กํ–ˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด a๋Š” p, b๋Š” q, c๋Š” r์— ๋Œ€์‘์‹œ์ผœ ์•”ํ˜ธํ™”ํ•œ ์ „์‹ ์„ ๋ณด๋‚ผ ๋•Œ โ€˜abcโ€™๋Š” โ€˜pqrโ€™๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ํ•ด๋… ์ฝ”๋“œ๊ฐ€ ๋‹ด๊ธด ๋ฌธ์„œ๋Š” ์‚ฌ๋žŒ์„ ์จ์„œ ๋ฐ˜๋Œ€ํŽธ์— ๋ฏธ๋ฆฌ ์ „๋‹ฌํ•ด๋‘๊ณ , ์ด๊ฒƒ์„ ์ฐธ๊ณ ํ•ด์„œ ์ „์‹ ์„ ํ•ด๋…ํ–ˆ์Šต๋‹ˆ๋‹ค.

์—๋‹ˆ๊ทธ๋งˆ๋Š” ๊ฐ ์•ŒํŒŒ๋ฒณ์ด ๋Œ€์‘ํ•˜๋Š” ๋‹ค๋ฅธ ์•ŒํŒŒ๋ฒณ์˜ ์„ธํŠธ์— ๋”ฐ๋ผ ๊ฐ™์€ ๋‹จ์–ด๋„ ๋‹ค๋ฅธ ์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์•”ํ˜ธ์˜ ์•ŒํŒŒ๋ฒณ์„ ํ‚ค๋กœ, ๋Œ€์‘ํ•˜๋Š” ์•ŒํŒŒ๋ฒณ์„ ๊ฐ’์œผ๋กœ ์ €์žฅํ•œ ๋‘ ๊ฐœ์˜ ๊ฐ์ฒด signal1 ๊ณผ signal2๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์•”ํ˜ธ๋ฌธ์˜ ํ˜•ํƒœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

01011 eowxvqp

์šฐ์„  01011์€ 5๊ฐœ์˜ 0๊ณผ 1๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ์•”ํ˜ธ๋ฌธ์ด 5๊ฐœ์˜ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์•”ํ˜ธ๋ฌธ์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์ด ์•”ํ˜ธ๋ฌธ์€ 5๋ฒˆ์งธ ์•ŒํŒŒ๋ฒณ์ธ v๊นŒ์ง€๋งŒ ํ•ด์„ํ•˜๋ฉฐ, 6๋ฒˆ์งธ ์•ŒํŒŒ๋ฒณ์ธ q๋ถ€ํ„ฐ๋Š” ํ•ด์„ํ•˜์ง€ ์•Š๊ณ  ๋ฒ„๋ฆฝ๋‹ˆ๋‹ค.

์•ž์— 0๊ณผ 1์€ ๊ฐ™์€ ์ž๋ฆฌ์˜ ์•ŒํŒŒ๋ฒณ์„ ๊ฐ๊ฐ signal1์„ ์ด์šฉํ•˜์—ฌ ํ•ด์„ํ• ์ง€, signal2๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•ด์„ํ• ์ง€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

์œ„ ์˜ˆ์‹œ์—์„œ 0์— ๋Œ€์‘ํ•˜๋Š” e, w๋Š” signal1์„ ์ด์šฉํ•˜์—ฌ ํ•ด์„ํ•˜๊ณ  1์— ๋Œ€์‘ํ•˜๋Š” o, x, v๋Š” signal2๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•ด์„ํ•ฉ๋‹ˆ๋‹ค.

signal1์—์„œ e์™€ w๋Š” ๊ฐ๊ฐ e์™€ i์— ๋Œ€์‘ํ•˜๋ฉฐ, signal2์—์„œ o, x, v๋Š” ๊ฐ๊ฐ l,c,e์— ๋Œ€์‘ํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์˜ˆ์‹œ์˜ ์•”ํ˜ธ๋ฌธ์„ ํ•ด์„ํ•˜๋ฉด elice๊ฐ€ ๋˜๋ฉฐ ์ด๋ฅผ ์ถœ๋ ฅํ•˜์‹œ๋ฉด ์ •๋‹ต์ž…๋‹ˆ๋‹ค.

์ด์ฒ˜๋Ÿผ ์•”ํ˜ธ๋ฌธ์„ ๋ฌธ์ž์—ด๋กœ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ์ด๋ฅผ ์›๋ฌธ์œผ๋กœ ํ•ด์„ํ•œ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์™„์„ฑํ•˜์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • ํ•ด์„ํ•ด์•ผํ•  ์•”ํ˜ธ๋ฌธ ๋งค๊ฐœ๋ณ€์ˆ˜ code๋ฅผ ์ž…๋ ฅ ๋ฐ›์Šต๋‹ˆ๋‹ค.

  • ์•”ํ˜ธ๋ฌธ์„ ํ•ด์„ํ•œ ํ›„ solution ํ•จ์ˆ˜ ์•ˆ์—์„œ return ํ•ด์ฃผ์„ธ์š”.

์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ์ถœ๋ ฅ
01011 eowxvqpelice
111 zmgaawant

๋‚˜์˜ ํ’€์ด

// ์•”ํ˜ธ์˜ ์•ŒํŒŒ๋ฒณ์„ ํ‚ค๋กœ, ๋Œ€์‘ํ•˜๋Š” ์›๋ฌธ์˜ ์•ŒํŒŒ๋ฒณ์„ ๊ฐ’์œผ๋กœ ์ €์žฅํ•œ ๋”•์…”๋„ˆ๋ฆฌ์ž…๋‹ˆ๋‹ค.
var signal1 = {
  a: 'n',
  b: 'd',
  c: 'a',
  d: 'b',
  e: 'e',
  f: 'l',
  g: 'j',
  h: 'o',
  i: 'z',
  j: 'u',
  k: 'y',
  l: 'v',
  m: 'w',
  n: 'q',
  o: 'x',
  p: 'r',
  q: 'p',
  r: 'f',
  s: 'g',
  t: 't',
  u: 'm',
  v: 'h',
  w: 'i',
  x: 'c',
  y: 'k',
  z: 's',
};

var signal2 = {
  a: 'z',
  b: 'y',
  c: 'x',
  d: 'w',
  e: 'v',
  f: 'u',
  g: 't',
  h: 's',
  i: 'r',
  j: 'q',
  k: 'p',
  l: 'o',
  m: 'n',
  n: 'm',
  o: 'l',
  p: 'k',
  q: 'j',
  r: 'i',
  s: 'h',
  t: 'g',
  u: 'f',
  v: 'e',
  w: 'd',
  x: 'c',
  y: 'b',
  z: 'a',
};

let splitMehod = function (word, seperator) {
  let result = [];
  let currentWord = '';

  if (seperator === '') {
    for (let i = 0; i < word.length; i++) {
      result.push(word[i]);
    }
  } else {
    for (let i = 0; i < word.length; i++) {
      if (word[i] === seperator) {
        result.push(currentWord);
        currentWord = '';
      } else {
        currentWord += word[i];
      }
    }
    result.push(currentWord);
  }
  return result;
};

// ์ง€์‹œ์‚ฌํ•ญ์„ ์ฐธ๊ณ ํ•˜์—ฌ solution ํ•จ์ˆ˜ ์•ˆ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.
function solution(code) {
  let codeSplit = splitMehod(code, ' ');
  let codeNum = codeSplit[0];
  let interpritChar = codeSplit[1];

  let answer = '';
  for (let i = 0; i < codeNum.length; i++) {
    if (codeNum[i] === '0') {
      answer += signal1[interpritChar[i]];
    } else {
      answer += signal2[interpritChar[i]];
    }
  }
  return answer;
}

ํ’€์ด ์ ‘๊ทผ

  1. ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ์ž…๋ ฅ ๊ฐ’ ์ฒซ ๋ฒˆ์งธ ๋ถ€๋ถ„์˜ ์ฝ”๋“œ๋ฅผ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ์—ฐ๊ด€๋œ ๋‘ ๊ฐ์ฒด ์กด์žฌ.

  2. ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์„ ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ„์–ด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์‹ถ๊ธฐ์— split ๋ฉ”์†Œ๋“œ ๊ตฌํ˜„

  3. ํ•ด์„ํ•ด์•ผ ํ•  ์ฝ”๋“œ์™€ ๋ฌธ์ž๋ฅผ ๊ฐœ๋ณ„ ๋ณ€์ˆ˜์— ์ €์žฅ

  4. ์กฐ๊ฑด์„ ํ†ตํ•ด codeNum์˜ ์ˆซ์ž์— ๋งž๊ฒŒ ๊ฐ์ฒด์— ์กด์žฌํ•˜๋Š” ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ answer์— ๋”ํ•จ.


์—˜๋ฆฌ์ŠคํŠธ๋ž™ ํ”„๋ฆฌํŠธ๋ž™ ๊ณผ์ • ๋ฌธ์ œ #7

[์—˜๋ฆฌ์Šค ํ”„๋ฆฌํŠธ๋ž™] : ๋ฌธ์ž์˜ ๋นˆ๋„ ์กฐ์‚ฌํ•˜๊ธฐ.

๋ฌธ์ œ ์„ค๋ช…
๋ฌธ์ž์—ด์€ ํ•˜๋‚˜์ด์ƒ์˜ ๋ฌธ์ž๋“ค๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜์–ด ๋ฌธ์žฅ์˜ ๊ฒฝ์šฐ a๋ถ€ํ„ฐ z๊นŒ์ง€์˜ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋Ÿฐ ์˜์–ด๋ฌธ์žฅ์—์„œ ์•ŒํŒŒ๋ฒณ๋ณ„๋กœ ๊ฐฏ์ˆ˜๋ฅผ ์กฐ์‚ฌํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ”„๋กœ๊ทธ๋žจ์„ ์ œ์ž‘ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์ง€์‹œ์‚ฌํ•ญ์— ๋งž์ถฐ ํ”„๋กœ๊ทธ๋žจ์„ ์™„์„ฑํ•˜์„ธ์š”.

์šฐ๋ฆฌ๋Š” ๊ฐ•์˜์—์„œ ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด์„œ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค. ๊ฐ•์˜์—์„œ ๋ฐฐ์šด ๋ฉ”์„œ๋“œ ์™ธ์—๋„ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํŽธ๋ฆฌํ•œ ๋ฉ”์„œ๋“œ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

๊ฐ•์˜์—์„œ ๋ฐฐ์šฐ์ง€ ์•Š์€ ์ƒˆ๋กœ์šด ๋ฉ”์„œ๋“œ๋ฅผ ํžŒํŠธ๋ฅผ ๋ณด๊ณ  ํ™œ์šฉํ•ด๋ณด์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • str ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ถ€ํ„ฐ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. (๋ฌธ์ž์—ด์—๋Š” ์•ŒํŒŒ๋ฒณ ๋ฐ ๊ณต๋ฐฑ๋งŒ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.)
    My name is Elice
  • ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    my name is elice
  • ๊ฐ ์•ŒํŒŒ๋ฒณ์ด ๋“ฑ์žฅํ•œ ํšŸ์ˆ˜๋ฅผ alpha_cnt์— ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.
  • solution ํ•จ์ˆ˜ ์•ˆ์—์„œ alpha_cnt ๊ฐ์ฒด๋ฅผ return ํ•˜์„ธ์š”.

์ž…์ถœ๋ ฅ ์˜ˆ

์ถœ๋ ฅ
{ m: 2, y: 1, n: 1, a: 1, e: 3, i: 2, s: 1, l: 1, c: 1 }

๋‚˜์˜ ํ’€์ด

let splitMethod = function (word, seperater) {
  let result = [];
  let currentWord = '';

  if (seperater === '') {
    for (let i = 0; i < word.length; i++) {
      result.push(word[i]);
    }
  } else {
    for (let i = 0; i < word.length; i++) {
      if (word[i] === seperater) {
        result.push(currentWord);
        currentWord = '';
      } else {
        currentWord += word[i];
      }
    }
    result.push(currentWord);
  }

  return result;
};

let lowCaseAlpa = function (arr) {
  let result = [];

  for (let i = 0; i < arr.length; i++) {
    result.push(arr[i].toLowerCase());
  }
  return result;
};

let theCharLine = function (arr) {
  let result = '';
  for (let i = 0; i < arr.length; i++) {
    result += arr[i];
  }
  return result;
};

// ์ง€์‹œ์‚ฌํ•ญ์„ ์ฐธ๊ณ ํ•˜์—ฌ solution ํ•จ์ˆ˜ ์•ˆ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.
function solution(str) {
  let strSplit = splitMethod(str, ' ');
  let lowCase = lowCaseAlpa(strSplit);
  let theLine = theCharLine(lowCase);

  let answer = {};
  for (let i = 0; i < theLine.length; i++) {
    let lineItem = theLine[i];

    // ์กด์žฌํ•˜๋Š”์ง€ ์•ˆ ํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์กฐ๊ฑด ์—ฌ๋ถ€.
    if (!answer[lineItem]) {
      answer[lineItem] = 1;
    } else answer[lineItem] += 1;
  }
  return answer;
}

ํ’€์ด ์ ‘๊ทผ

  1. ๊ฐ ๊ณต๋ฐฑ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž๋“ค์„ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜

  2. ๋ฐ˜ํ™˜๋œ ๋ฐฐ์—ด๋“ค ์ˆœํšŒํ•˜์—ฌ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜

  3. ๋ฐฐ์—ด์„ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜.

  4. answer ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด for๋ฐ˜๋ณต์„ ํ†ตํ•ด answer์—๋‹ค ์†์„ฑ๋ถ€์—ฌ.

  5. ์กฐ๊ฑด์„ ํ†ตํ•ด ํ•ด๋‹น ์†์„ฑ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด 1์„ ๋”ํ•˜๊ณ  else๋ฉด +=1์„ ์นด์šดํŠธํ•จ.


    ์—˜๋ฆฌ์ŠคํŠธ๋ž™ ํ”„๋ฆฌํŠธ๋ž™ ๊ณผ์ • ๋ฌธ์ œ #8

    [์—˜๋ฆฌ์Šค ํ”„๋ฆฌํŠธ๋ž™] : ๋‹น๊ทผ ํƒ์ง€๊ธฐ

๋ฌธ์ œ ์„ค๋ช…
๋•… ์†์— ์ˆจ๊ฒจ์ ธ ์žˆ๋Š” ๋‹น๊ทผ์„ ์ฐพ๊ธฐ ์ข‹์•„ํ•˜๋Š” ์—˜๋ฆฌ์Šค ํ† ๋ผ๋Š” ๋‹น๊ทผ์„ ํƒ์ง€ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹น๊ทผ ํƒ์ง€๊ธฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์™ผ์ชฝ ํ˜น์€ ์˜ค๋ฅธ์ชฝ์œผ๋กœ๋งŒ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ธธ์ด๊ฐ€ 5์ธ ๊ธธ ์–ด๋””์ธ๊ฐ€์— ๋–จ์–ด์กŒ์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์•„๋ž˜์™€ ๊ฐ™์ด 5์นธ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ธธ์ด ์žˆ๊ณ  O์€ ๋‹น๊ทผ์ด ์žˆ๋Š” ๊ณณ, X์€ ๋‹น๊ทผ์ด ์—†๋Š” ๊ณณ์ด๋ฉฐ ์—˜๋ฆฌ์Šค ํ† ๋ผ๊ฐ€ ์™ผ์ชฝ์—์„œ ์„ธ๋ฒˆ์งธ ์นธ์— ๋–จ์–ด์กŒ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์™ผ์ชฝ ๋์œผ๋กœ ์ด๋™ํ•˜๊ฒŒ ๋˜๋ฉด ์ด 1๊ฐœ์˜ ๋‹น๊ทผ์„ ํš๋“ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ํ•œ ๊ฒฝ์šฐ 2๊ฐœ์˜ ๋‹น๊ทผ์„ ํš๋“ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

OXXOO

O์™€ X๋Š” ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž O, X๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ์‚ฌํ•ญ

  • ๋งค๊ฐœ๋ณ€์ˆ˜ a์—์„œ O, X๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜ b์—์„œ๋Š” ์œ„์น˜ ๊ฐ’์ธ ์ˆซ์ž๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค.

  • ๋–จ์–ด์ง„ ์œ„์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‹น๊ทผ์„ ์ตœ๋Œ€ํ•œ ๋งŽ์ด ํš๋“ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ(์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ)์„ ์ถœ๋ ฅํ•˜์„ธ์š”.

  • ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ์˜ ๋‹น๊ทผ์˜ ์ˆ˜๊ฐ€ ๋™์ผํ•œ ๊ฒฝ์šฐ ๋™์ผ์„ ์ถœ๋ ฅํ•˜์„ธ์š”.

  • ์ถœ๋ ฅ์€ solution ํ•จ์ˆ˜ ์•ˆ์—์„œ return ํ•˜์„ธ์š”.

์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ์ถœ๋ ฅ
"O X X O O", 3์˜ค๋ฅธ์ชฝ

๋‚˜์˜ ํ’€์ด

let splitMethod = function (word, seperator) {
  let result = [];
  let currentWord = '';

  if (seperator === '') {
    for (let i = 0; i < word.length; i++) {
      result.push(word[i]);
    }
  } else {
    for (let i = 0; i < word.length; i++) {
      if (word[i] === seperator) {
        result.push(currentWord);
        currentWord = '';
      } else {
        currentWord += word[i];
      }
    }
    result.push(currentWord);
  }
  return result;
};

// ์ง€์‹œ์‚ฌํ•ญ์„ ์ฐธ๊ณ ํ•˜์—ฌ solution ํ•จ์ˆ˜ ์•ˆ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.
function solution(a, b) {
  let splitOX = splitMethod(a, ' ');

  let leftCount = 0;
  for (let i = b-1; i >= 0; i--) {
    if (splitOX[i] === 'O') leftCount += 1;
  }

  let rightCount = 0;
  for (let i = b-1; i < splitOX.length; i++) {
    if (splitOX[i] === 'O') rightCount += 1;
  }
  
  
  return leftCount > rightCount ? '์™ผ์ชฝ' : (leftCount < rightCount ? '์˜ค๋ฅธ์ชฝ' : '๋™์ผ');
}

ํ’€์ด ์ ‘๊ทผ

  1. OX ๋ฌธ์ž์—ด ๊ณต๋ฐฑ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
  2. ํ•จ์ˆ˜ ์ธ์ž b์˜ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ for ์กฐ๊ฑด i์˜ ๊ฐ’ ํ˜•์„ฑ
  3. ๋‹น๊ทผ์ด ๋ช‡๊ฐœ ์กด์žฌํ•˜๋Š”์ง€ ๊ฐ ๋ณ€์ˆ˜์— ์นด์šดํŠธ ๊ฐ’ ์ €์žฅ
  4. ์‚ผํ•ญ์—ฐ์‚ฐ์ž์˜ ์ค‘๋ณต์œผ๋กœ ์„ธ๊ฐ€์ง€ return ๊ฐ’ ์กฐ๊ฑด ์„ค์ •

์—˜๋ฆฌ์ŠคํŠธ๋ž™ ํ”„๋ฆฌํŠธ๋ž™ ๊ณผ์ • ๋ฌธ์ œ #9

[์—˜๋ฆฌ์Šค ํ”„๋ฆฌํŠธ๋ž™] : ๋”์น˜ํŽ˜์ด ๊ณ„์‚ฐํ•˜๊ธฐ

๋ฌธ์ œ ์„ค๋ช…
A, B, C ์„ธ๋ช…์˜ ์นœ๊ตฌ๋Š” ์ ์‹ฌ์„ ํ•จ๊ป˜ ๋จน๊ณ  ๊ฐ์ž ๋จน์€ ๋ฉ”๋‰ด์— ๋”ฐ๋ผ ๊ณ„์‚ฐํ•˜๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.

์„ธ๋ช…์˜ ์นœ๊ตฌ๊ฐ€ ๊ฐ๊ฐ ๋ฉ”๋‰ด๋ฅผ ํ•˜๋‚˜์”ฉ๋งŒ ์ฃผ๋ฌธํ–ˆ๋‹ค๋ฉด ์‰ฝ๊ฒŒ ๊ณ„์‚ฐ์ด ๊ฐ€๋Šฅํ–ˆ๊ฒ ์ง€๋งŒ, B๋Š” 2๊ฐœ์˜ ๋ฉ”๋‰ด๋ฅผ ์ฃผ๋ฌธํ•˜๊ณ  C๋Š” 3๊ฐœ๋ฅผ ์ฃผ๋ฌธํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ฑฐ๊ธฐ์— ์„ธ๋ช…์ด ํ•จ๊ป˜ ๋จน๋Š” ์‚ฌ์ด๋“œ๋ฉ”๋‰ด๊นŒ์ง€ ํฌํ•จ๋˜์–ด ์žˆ์–ด์„œ ์ด๋ฅผ ๋ฐ˜์˜ํ•ด์„œ ๊ฐ์ž ์ง€๋ถˆํ•  ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“œ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์ง€์‹œ์‚ฌํ•ญ์„ ์ฐธ๊ณ ํ•˜์—ฌ A, B, C ๊ฐ€ ๊ฐ๊ฐ ์ง€๋ถˆํ•ด์•ผํ•˜๋Š” ๊ธˆ์•ก์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ œ์ž‘ํ•˜์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • menu ๋ผ๋Š” ๊ฐ์ฒด์— ๋ฉ”๋‰ด๋ช…์„ ํ‚ค๋กœ, ๋ฉ”๋‰ด์˜ ๊ฐ€๊ฒฉ์„ ๊ฐ’์œผ๋กœ ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์ด๋“ค์ด ์ฃผ๋ฌธํ•œ ๋‚ด์—ญ์€ ๋จน์€์‚ฌ๋žŒ, ๋ฉ”๋‰ด์ด๋ฆ„, ์ˆ˜๋Ÿ‰์˜ ํ˜•ํƒœ๋กœ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์„ธ๋ช…์ด ๋‹ค๊ฐ™์ด ๋จน์€ ๋ฉ”๋‰ด๋ผ๋ฉด K๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

  • ๊ฐ์ž ์ง€๋ถˆํ•  ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜์—ฌ ๊ฐ์ฒด์— ๋‹ด์•„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

  • ์ถœ๋ ฅํ•  ๊ฐ’์€ solution ํ•จ์ˆ˜์—์„œ return ํ•ด์ฃผ์„ธ์š”.

์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ์ถœ๋ ฅ
[
[ "A", "๋ผ๋ฉด", 1],
[ "B", "๊น€๋ฐฅ", 2],
[ "B", "๋–ก๋ณถ์ด", 1],{ A: 7000, B: 10000, C: 2000 }
[ "K", "ํŠ€๊น€์„ธํŠธ", 1],
[ "C", "์ฝœ๋ผ", 1 ]
];

๋‚˜์˜ ํ’€์ด

let theObject = function (arr) {
  let answer = [];

  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < 1; j++) {
      let payCount = {
        name: arr[i][0],
        food: arr[i][1],
        foodCnt: arr[i][2],
      };
      answer.push(payCount);
    }
  }
  return answer;
};

function solution(input) {
  var menu = {
    ๋–ก๋ณถ์ด: 5000,
    ๊น€๋ฐฅ: 2000,
    ํŠ€๊น€์„ธํŠธ: 3000,
    ์ˆœ๋Œ€: 4000,
    ๋ผ๋ฉด: 6000,
    ์ฝœ๋ผ: 1000,
    ์‚ฌ์ด๋‹ค: 1000,
  };
  theObject(input);

  let answer = {
    A: 0,
    B: 0,
    C: 0,
    K: 0,
  };

  for (let i = 0; i < input.length; i++) {
    let objectName = input[i][0]; // "A", "B", "C" ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
    let foodName = input[i][1];
    let foodCnt = input[i][2];

    answer[objectName] += menu[foodName] * foodCnt;
  }
  if (answer.K) {
    answer.A += Math.floor(answer.K / 3);
    answer.B += Math.floor(answer.K / 3);
    answer.C += Math.floor(answer.K / 3);
  }
  delete answer.K;

  return answer;
}

ํ’€์ด ์ ‘๊ทผ

  1. ์ž…๋ ฅ๊ฐ’ ๊ฐ ๋ฐฐ์—ด์— ๋‹ด๊ธด ์ด๋ฆ„, ์Œ์‹, ๊ฐœ์ˆ˜๋ฅผ ๊ฐ์ฒดํ™” ํ•˜์—ฌ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜

  2. theObject() ํ•จ์ˆ˜๋กœ ๋ฐ˜ํ™˜๋œ ๋ฐฐ์—ด ๊ฐ์ฒด๋ฅผ for๋ฌธ์„ ํ†ตํ•ด ์ˆœํ™˜ํ•˜์—ฌ
    objectName
    foodName
    foodCnt ๋ณ€์ˆ˜์— ๊ฐ ๊ฐ์ฒด ์†์„ฑ๊ฐ’์— ๋งž๊ฒŒ ์ €์žฅ.

  3. answer ๊ฐ์ฒด์— ์—ฐ๊ด€๋˜๋Š” ์†์„ฑ์— ๋งž๊ฒŒ ๊ฐ€๊ฒฉ์„ ๊ฐ’์„ ์ €์žฅ

  4. k๊ฐ€ ์กด์žฌํ•˜๋ฉด ์ œํ•œ์‚ฌํ•ญ ์ธ์›์— ๋งž๊ฒŒ 3์œผ๋กœ ๋‚˜๋ˆˆ ๊ฐ€๊ฒฉ์„ ๊ฐ๊ฐ ๋‚˜๋ˆ”.

  5. ๋งˆ์ง€๋ง‰์œผ๋กœ k๋ฅผ ์‚ญ์ œํ•˜๊ณ 

  6. answer ๊ฐ์ฒด ๋ฐ˜ํ™˜


    ์—˜๋ฆฌ์ŠคํŠธ๋ž™ ํ”„๋ฆฌํŠธ๋ž™ ๊ณผ์ • ๋ฌธ์ œ #10

    [์—˜๋ฆฌ์Šค ํ”„๋ฆฌํŠธ๋ž™] : ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ ์••์ถ•ํ•˜๊ธฐ

๋ฌธ์ œ ์„ค๋ช…
๋ฐ์ดํ„ฐ๋ฅผ ์••์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ๋‹ค์–‘ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

๊ทธ ์ค‘์— Run-length encoding (์ดํ•˜ RLE)์€ ์—ฐ์†๋˜์–ด ๊ฐ™์€ ๋ฌธ์ž๊ฐ€ ๋ฐ˜๋ณต๋ ๋•Œ ์–ด๋–ค ๋ฌธ์ž๊ฐ€ ๋ช‡๋ฒˆ ๋ฐ˜๋ณต๋˜๋Š”์ง€๋กœ ์••์ถ•ํ•˜์—ฌ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, โ€œaaaaaabbbcccccbbbbbโ€ ๋ผ๋Š” 19๊ฐœ์˜ ๋ฌธ์ž ๋ฐ์ดํ„ฐ๋Š” a๊ฐ€ 6๋ฒˆ, b๊ฐ€ 3๋ฒˆ, c๊ฐ€ 5๋ฒˆ, b๊ฐ€ 5๋ฒˆ ์—ฐ์†๋˜์–ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ์ด๋Š” โ€œa6b3c5b5โ€ ์ด๋ ‡๊ฒŒ 8๊ฐœ์˜ ๋ฌธ์ž๋กœ ์••์ถ•ํ•˜์—ฌ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ โ€œaabbโ€ ์ด๋ ‡๊ฒŒ 2๋ฒˆ ์ดํ•˜๋กœ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž๋Š” โ€œa2b2โ€ ์ด๋ ‡๊ฒŒ ๋ฐ”๊ฟ”์„œ ํ‘œํ˜„ํ•ด๋„ ๊ธธ์ด๋Š” ์ค„์–ด๋“ค์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Š” ๊ทธ๋Œ€๋กœ โ€œaabbโ€๋กœ ํ‘œํ˜„ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

์ฆ‰, โ€œaaabbccccaabbbbโ€์ด๋Ÿฐ ๋ฌธ์ž์—ด์„ ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•˜์—ฌ ์••์ถ•ํ•˜๋ฉด โ€œa3bbc4aab4โ€ ์ด๋ ‡๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•˜์—ฌ โ€˜Aโ€™๋ถ€ํ„ฐ โ€˜Zโ€™๊นŒ์ง€ 26๊ฐœ์˜ ๋Œ€๋ฌธ์ž ์•ŒํŒŒ๋ฒณ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด์„ ์••์ถ•ํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์ง€์‹œ์‚ฌํ•ญ์— ๋”ฐ๋ผ ํ”„๋กœ๊ทธ๋žจ๋ฅผ ์™„์„ฑ์‹œํ‚ค์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • A๋ถ€ํ„ฐ Z๊นŒ์ง€ ๋Œ€๋ฌธ์ž ์•ŒํŒŒ๋ฒณ์œผ๋กœ๋งŒ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด์„ ์ธ์ž๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค.

  • ์ด ๋ฌธ์ž์—ด์„ ์œ„์—์„œ ์„ค๋ช…ํ•œ RLE ๋ฐฉ์‹์œผ๋กœ ์••์ถ•ํ•ฉ๋‹ˆ๋‹ค.

  • ์••์ถ•๋œ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

  • ์ถœ๋ ฅ์€ solution ํ•จ์ˆ˜ ์•ˆ์—์„œ return ํ•˜์„ธ์š”.

์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ์ถœ๋ ฅ
AAAAABBCCCDDDZZZWWA5BBC3D3Z3WW

๋‚˜์˜ ํ’€์ด

// ์ง€์‹œ์‚ฌํ•ญ์„ ์ฐธ๊ณ ํ•˜์—ฌ solution ํ•จ์ˆ˜ ์•ˆ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.
function solution(str) {
  let upperStr = str.toUpperCase();
  let sameCase = {};

  for (let i = 0; i < upperStr.length; i++) {
    let caseItem = upperStr[i];
    if (!sameCase[caseItem]) {
      sameCase[caseItem] = 1;
    } else {
      sameCase[caseItem] += 1;
    }
  }
  console.log(sameCase); //  { A: 5, B: 2, C: 3, D: 3, Z: 3, W: 2 }
  
  let outputStr = '';
  const keys = Object.keys(sameCase);
  console.log(keys); // [ 'A', 'B', 'C', 'D', 'Z', 'W' ]
  
  for (let i = 0; i < keys.length; i++) {
    const key = keys[i];

    outputStr += key;
    
    if (sameCase[key] > 2) {
      outputStr += sameCase[key];
    } else {
      outputStr += key;
    }
  }

  return outputStr;
}

ํ’€์ด ์ ‘๊ทผ

  1. ๊ฐ๋ฌธ์ž์—ด์„ ์ œํ•œ์‚ฌํ•ญ์— ๋งž๊ฒŒ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜
  2. ๋ฌธ์ž ๋นˆ๋„ ๊ตฌํ–ˆ๋˜ ๋ฐฉ์‹ ์ฒ˜๋Ÿผ ๊ฐ ๋ฌธ์ž๋ณ„ ์†์„ฑ์„ ๊ฐ€์ง„ ๊ฐ์ฒด ์ €์žฅ
  3. ๊ฐ์ฒด ์†์„ฑ ์กด์žฌ ์œ ๋ฌด์— ๋”ฐ๋ผ ์นด์šดํŠธ๋ฅผ ๋”ํ•จ
  4. ๋งŒ๋“ค์–ด์ง„ sameCase = { A: 5, B: 2, C: 3, D: 3, Z: 3, W: 2 }
  5. Object.keys()๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด์˜ ํ‚ค๊ฐ’์„ ๊ฐ€์ง„ ๋ฐฐ์—ด ๋ฐ˜ํ™˜.
  6. keys = [ 'A', 'B', 'C', 'D', 'Z', 'W' ]
  7. keys๋ฅผ ์ˆœํšŒํ•˜์—ฌ sameCase์— ์กด์žฌํ•˜๋Š” ์†์„ฑ ๊ฐ’์ด 2๋ณด๋‹ค ํฐ์ง€ ์กฐ๊ฑด์— ๋”ฐ๋ผ
    return ํ•  outputStr์— ๋ฌธ์ž๋ฅผ ๋”ํ•จ.

์—˜๋ฆฌ์ŠคํŠธ๋ž™ ํ”„๋ฆฌํŠธ๋ž™ ๊ณผ์ • ๋ฌธ์ œ #11

[์—˜๋ฆฌ์Šค ํ”„๋ฆฌํŠธ๋ž™] : ๊ด„ํ˜ธ์˜ ์ง

๋ฌธ์ œ ์„ค๋ช…
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ด„ํ˜ธ๋Š” ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ค‘ [ ], ( ), { }๋Š” ์ž์ฃผ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ• ๋•Œ๋Š” ํ•ญ์ƒ ์ง์ด ๋งž๋Š” ๊ฒƒ์„ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด { ( ) [ ] }๋Š” ์ง์ด ๋งž์ง€๋งŒ { [ } ] .( ) ๋Š” ์ง์ด ๋งž์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ด๊ฒƒ์„ ํ™•์ธํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ”„๋กœ๊ทธ๋žจ์„ ๋ฐฐ์—ด์„ ํ™œ์šฉํ•ด์„œ ๋งŒ๋“ค์–ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

ํžŒํŠธ๋กœ ์ œ๊ณตํ•˜๋Š” ๋‚ด์šฉ๊ณผ ์ง€์‹œ์‚ฌํ•ญ์„ ์ฝ๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ์™„์„ฑํ•˜์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • ์˜์–ด, ์ˆซ์ž, ์‚ฌ์น™์—ฐ์‚ฐ ๊ธฐํ˜ธ +*-/ ๊ทธ๋ฆฌ๊ณ  ๊ด„ํ˜ธ๋“ค๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์žฅ์„ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค.
(a+b[a])+[{(b*e)/(a+q)}]
  • ๋ฌธ์ž์—ด์—์„œ ๊ด„ํ˜ธ๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž๋Š” ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
([])[{()()}]
  • ๋ฐฐ์—ด์˜ pop()๊ณผ push()๋ฅผ ์ด์šฉํ•ด์„œ ๊ด„ํ˜ธ์˜ ์ง์ด ๋งž๋Š”์ง€ ๋งž์ง€ ์•Š๋Š”์ง€๋ฅผ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค.

  • ์ง์ด ๋งž๋‹ค๋ฉด ์ •์ƒ, ์ง์ด ๋งž์ง€ ์•Š๋Š” ๋ถ€๋ถ„์ด ํ•˜๋‚˜๋ผ๋„ ์žˆ๋‹ค๋ฉด ๋น„์ •์ƒ์ด๋ผ๊ณ  ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์œ„ ๋ฌธ์žฅ์€ ์ง์ด ๋งž์œผ๋ฏ€๋กœ ์ •์ƒ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ์ถœ๋ ฅ
(a+b[a])+[{(b*e)/(a+q)}]์ •์ƒ

๋‚˜์˜ ํ’€์ด

// ์ง€์‹œ์‚ฌํ•ญ์„ ์ฐธ๊ณ ํ•˜์—ฌ solution ํ•จ์ˆ˜ ์•ˆ์— ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.
function solution(string) {
  const stack = [];
  // ์—ฌ๋Š” ๊ด„ํ˜ธ์™€ ๋‹ซ๋Š” ๊ด„ํ˜ธ์˜ ๋Œ€์‘์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  const bracketsMap = {
    ')': '(',
    ']': '[',
    '}': '{',
  };

  for (let i = 0; i < string.length; i++) {
    const char = string[i];
    if (char === '(' || char === '[' || char === '{') {
      // ์—ฌ๋Š” ๊ด„ํ˜ธ์ธ ๊ฒฝ์šฐ ์Šคํƒ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
      stack.push(char);
    } else if (char === ')' || char === ']' || char === '}') {
      // ๋‹ซ๋Š” ๊ด„ํ˜ธ์ธ ๊ฒฝ์šฐ ์Šคํƒ์—์„œ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๊บผ๋ƒ…๋‹ˆ๋‹ค.
      if (stack.length === 0 || stack.pop() !== bracketsMap[char]) {
        // ์Šคํƒ์ด ๋น„์–ด์žˆ๊ฑฐ๋‚˜ ์ง์ด ๋งž์ง€ ์•Š๋Š” ๊ฒฝ์šฐ false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
        return '๋น„์ •์ƒ';
      }
    }
  }

  // ๋ชจ๋“  ์ž…๋ ฅ๊ฐ’์„ ์ฒ˜๋ฆฌํ•œ ํ›„์— ์Šคํƒ์ด ๋น„์–ด์žˆ๋‹ค๋ฉด, ๊ด„ํ˜ธ ์ง์ด ๋งž๋Š” ๊ฒƒ์œผ๋กœ ํŒ๋ณ„ํ•ฉ๋‹ˆ๋‹ค.
  return stack.length === 0 ? '์ •์ƒ' : '๋น„์ •์ƒ';
}

ํ’€์ด ์ ‘๊ทผ

  1. ์—ฌ๋Š” ๊ด„ํ˜ธ์™€ ๋‹ซ๋Š” ๊ด„ํ˜ธ์˜ ๋Œ€์‘์„ ๊ฐ์ฒด๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค
  2. ์—ฌ๋Š” ๊ด„ํ˜ธ์ธ ๊ฒฝ์šฐ ์Šคํƒ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
  3. ๋‹ซ๋Š” ๊ด„ํ˜ธ์ธ ๊ฒฝ์šฐ ์Šคํƒ์—์„œ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๊บผ๋ƒ…๋‹ˆ๋‹ค
  4. ๋‹ซ๋Š” ๊ด„ํ˜ธ์ธ ๊ฒฝ์šฐ ์Šคํƒ์—์„œ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๊บผ๋ƒ…๋‹ˆ๋‹ค
  5. ์Šคํƒ์ด ๋น„์–ด์žˆ๊ฑฐ๋‚˜ ์ง์ด ๋งž์ง€ ์•Š๋Š” ๊ฒฝ์šฐ false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค
  6. ๋ชจ๋“  ์ž…๋ ฅ๊ฐ’์„ ์ฒ˜๋ฆฌํ•œ ํ›„์— ์Šคํƒ์ด ๋น„์–ด์žˆ๋‹ค๋ฉด, ๊ด„ํ˜ธ ์ง์ด ๋งž๋Š” ๊ฒƒ์œผ๋กœ ํŒ๋ณ„ํ•ฉ๋‹ˆ๋‹ค
profile
๋ฐฉ๋ฒ•์„ ์—ฐ๊ตฌํ•  ์ค„ ์•„๋Š” ๊ฐœ๋ฐœ์ž!

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 7์›” 31์ผ

์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค. ์ข‹์€ ๊ธ€ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ