๐Ÿ’ป TIL 2023.02.08

๊น€์˜์šฐ(Yeongwoo Kim)ยท2023๋…„ 2์›” 8์ผ
0
post-thumbnail

1. ๋’ค์— ์žˆ๋Š” ํฐ ์ˆ˜ ์ฐพ๊ธฐ(level 2)

- ๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/154539

- ์ฝ”๋“œ ์ž‘์„ฑ ์ „ ์ƒ๊ฐ

  1. ๋‹จ์ˆœํžˆ while ๋ฌธ์„ ๋Œ๋ฉด์„œ numList, firstNumber๋ฅผ ๋งŒ๋“ค๊ณ  numList.map์„ ํ†ตํ•ด firstNumber๋ณด๋‹ค ํฌ๋ฉด ๋ฉˆ์ถ”๊ณ  numberList๋ฅผ popํ•˜์ž๋Š” ์ƒ๊ฐ์ด์˜€๋‹ค.
  • ํ•ด๋‹น ์ƒ๊ฐ์€ ํ‹€๋ ธ๋‹ค

    • ์ฒซ๋ฒˆ์งธ
      javaScript์˜ es6 ํ•จ์ˆ˜ (์ฆ‰, ๊ณ ์ฐจํ•จ์ˆ˜ map, filter ...)๋Š” break๊ฐ€ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ ์ด์œ ๋Š” ๊ณต์‹๋ฌธ์„œ์—์„œ ์ฐพ์•˜๋‹ค.

      Description
      map calls a provided callback function once for each element in an array, in order, and constructs a new array from the results. callback is invoked only for indexes of the array which have assigned values (including undefined).
      ์š”์•ฝํ•˜์ž๋ฉด ์ฃผ์–ด์ง„ input array์˜ ๊ธธ์ด๋งŒํผ iterator๋ฅผ ๋Œ๋ฆฌ๋ฉด์„œ ๊ทธ๋•Œ๋งˆ๋‹ค callback ํ•จ์ˆ˜๋ฅผ ๋Œ๋ฆฐ๋‹ค. ์ฆ‰, callback์„ ํ˜ธ์ถœํ•œ ์ˆœ๊ฐ„ Loop์˜ ์—ญํ• ์€ ๋๋‚˜๊ณ  Loop์˜ ์ค‘๊ฐ„์— break๋ฅผ ๊ฑธ ์ˆ˜ ์—†๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

    • ๋‘๋ฒˆ์งธ
      ๊ณ ์ฐจํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฌธ์ œ์˜ numbers์˜ ํฌ๊ธฐ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ  ์ƒ๊ฐ ์—†์ด ์ฝ”๋“œ๋ฅผ ์งฐ๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ 1,000,000,000,000์œผ๋กœ ๋„ˆ๋ฌด ์ปค์ ธ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

      const solution = (numbers) => {
       let answer = [];
       while(numbers.length > 0){
           const firstNum = numbers[0];
           const numList = numbers.slice(1);
           let isBiggest = true;
           
           for(let i = 0; i < numList.length; i++){
               console.log(firstNum, numList[i])
               if(numList[i] > firstNum){
                   answer.push(numList[i]);
                   isBiggest = false;
                   numbers = numList;
               }
           }
           if(isBiggest === true) {
               answer.push(-1);
               numbers = numList;
           }
      
       }
       return answer;
      }

- ๊ตฌํ˜„ ์ค‘ ์ถ”๊ฐ€ ๋œ ํ•ญ๋ชฉ

  1. for๋ฌธ์„ ๋จผ์ € ์‚ฌ์šฉํ•˜์—ฌ ์•ˆ์—์„œ while๋ฌธ์˜ ์กฐ๊ฑด์„ ๋‹ฌ์•„๋ณด์ž๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.
    • stack ์—๋Š” numbers์˜ ์›์†Œ์˜ ์ธ๋ฑ์Šค๋“ค์ด ๋“ค์–ด๊ฐ„๋‹ค.
    • while ๋ฌธ์—์„œ๋Š” stack์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์˜ value์™€ numbers[i] ๊ฐ’๊ณผ ๋น„๊ตํ•˜์—ฌ popํ•ด์คŒ์œผ๋กœ์จ ๋’คํฐ์ˆ˜๊ฐ€ ์—†๋Š” ์ธ๋ฑ์Šค๊ฐ€ ๋‚จ๋„๋กํ•œ๋‹ค.
    • ๋งˆ์ง€๋ง‰์œผ๋กœ stack ์— ๋‚จ์•„์žˆ๋Š” ์ธ๋ฑ์Šค๋“ค์€ ๋’คํฐ์ˆ˜๊ฐ€ ์—†๋Š” ์›์†Œ๋“ค์ด๋ฏ€๋กœ ํ•ด๋‹น ์ธ๋ฑ์Šค์ž๋ฆฌ์—๋Š” -1๋กœ ์ฑ„์›Œ ๋„ฃ์Œ์œผ๋กœ ์ตœ์ข…์ ์œผ๋กœ๋Š” answer ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

- ์ฝ”๋“œ

const solution = (numbers) => {
    let answer = new Array(numbers.length).fill(0);
    let stack = []
    for(let i = 0; i < numbers.length; i++){
            while (stack.length > 0 && numbers[stack[stack.length-1]] < numbers[i]){
                answer[stack.pop()] = numbers[i]
            }
            stack.push(i)
            
    }
    while (stack.length > 0){
            answer[stack.pop()] = -1
    }
    
    return answer;
}

2. ์‹œ์†Œ ์ง๊ฟ(level 2)

- ๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/152996

- ์ฝ”๋“œ ์ž‘์„ฑ ์ „ ์ƒ๊ฐ

  1. ์œ„์น˜์— ๋”ฐ๋ฅธ ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” 4๊ฐ€์ง€๋งŒ ์กด์žฌ ํ•œ๋‹ค (1,1) (2,3) (2,4) (3,4) ๊ฑฐ๋ฆฌ์— ๋”ฐ๋ฅธ ๊ฒฝ์šฐ
  2. ๊ฑฐ๋ฆฌ์— ๋”ฐ๋ฅธ ๋น„์œจ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜ ratio์— [1,3/2,2,4/3]์„ ์ƒ์„ฑํ•œ๋‹ค.
  3. weights๋ฅผ ์ •๋ ฌ์‹œ์ผœ ํฐ์ˆ˜๋ถ€ํ„ฐ store์— ๊ฐœ์ˆ˜๋ฅผ ๋„ฃ์–ด๊ฐ€๋ฉด์„œ ์ €์žฅํ•œ๋‹ค.

- ๊ตฌํ˜„ ์ค‘ ์ถ”๊ฐ€ ๋œ ํ•ญ๋ชฉ

  1. store[ํ˜„์žฌ๋ฌด๊ฒŒ]์— ๊ฐ’์ด ์—†์œผ๋ฉด 1์„ ๋„ฃ์–ด์ฃผ๊ณ  ์žˆ๋‹ค๋ฉด +1 ์„ ํ•ด์ฃผ๋Š” ์‹์œผ๋กœ ์ง„ํ–‰ํ•ด์•ผํ•œ๋‹ค.

- ์ฝ”๋“œ ์˜ˆ์‹œ [50,100,200,100] ๊ธฐ์ค€

  1. weights = [200,100,100,50]
  2. w = 200 store = { 200 : 1}
  3. w = 100 ํ•ด๋‹น ํ•จ์ˆ˜์— ์˜ํ•ด store[200]์— ๊ฐ’์ด ์กด์žฌ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— answer += store[200] ์ด๋œ๋‹ค.
    cal.forEach((i)=>{
          if( s = w * i, store[s] ){ //ํ•ด๋‹น ๋น„์œจ์„ ๊ณฑํ•œ ๊ฐ’์ด store์— ์กด์žฌํ•  ๊ฒฝ์šฐ
            answer += store[s];
          }
      });
  4. ํ•ด๋‹น ๋กœ์ง์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ ์ด 5์˜ ๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.

- ์ฝ”๋“œ

const solution = (weights) => {
  let answer = 0;
  const store = {};
  const ratio = [1, 3 / 2, 2, 4 / 3];

  weights
    .sort((a, b) => b - a)
    .forEach((w) => {
      let s;
      ratio.forEach((i) => {
        if (((s = w * i), store[s])) {
          answer += store[s];
        }
      });
      if (!store[w]) store[w] = 1;
      else store[w]++;
    });
  return answer;
};

3. ์ˆซ์ž ํƒ€์ž ๋Œ€ํšŒ(level 3)

- ๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/136797

- ์ฝ”๋“œ ์ž‘์„ฑ ์ „ ์ƒ๊ฐ

  1. ๋จผ์ € ๋ฌธ์ž์—ด์—์„œ 110์„ ๋ชจ๋‘ ์ฐพ์•„์„œ ์ œ๊ฑฐํ•œ๋‹ค.
  2. ๋ฌธ์ž์—ด์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋Œ๋ฉด์„œ, stack์˜ ๊ธธ์ด๊ฐ€ 2๋ณด๋‹ค ์ž‘๋‹ค๋ฉด, ๊ทธ๋ƒฅ stack์— ํ•ด๋‹น ๊ธ€์ž๋ฅผ push. ์•„๋‹ˆ๋ผ๋ฉด stack์„ 2๋ฒˆ popํ•ด์„œ 110์„ ์ด๋ฃจ๋Š” ์ง€ํ™•์ธํ•˜๊ณ , 110์ด๋ผ๋ฉด tmp์— 110์„ ๋”ํ•˜๊ณ , ์•„๋‹ˆ๋ผ๋ฉด ๋‹ค์‹œ stack์— ๋Œ๋ ค๋†“๋Š”๋‹ค.
  3. ๋ฐ˜๋ณต์ด ๋๋‚˜๋ฉด ๋‚จ์€ ๋ฌธ์ž์—ด tmp๋ฅผ returnํ•œ๋‹ค.
  4. returnํ•œ ๋ฌธ์ž์—ด์ด ๋น„์—ˆ๋‹ค๋ฉด str์„ ๊ทธ๋Œ€๋กœ pushํ•œ๋‹ค.
  5. ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ๋งˆ์ง€๋ง‰ 0์˜ ๋’ค์— ์ €์žฅํ•œ 110๋“ค์„ ๋„ฃ์–ด์ค€๋‹ค => ์ด์œ ๋Š” ๋ชจ๋“  110์„ ๋นผ๋ƒˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‚จ์€ stack์—๋Š” 11์ด ๋‘๊ฐœ ์ด์ƒ ๋‚˜์˜ฌ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

=> ์˜ˆ๋ฅผ ๋“ค์–ด ["0111101100011110"]์˜ ๊ฒฝ์šฐ ๋‚˜์™€์•ผํ•˜๋Š” ๊ฐ’์€ ["0011011011011011"]์ด๋‹ค.

- ๊ตฌํ˜„ ์ค‘ ์ถ”๊ฐ€ ๋œ ํ•ญ๋ชฉ

  1. ๋งˆ์ง€๋ง‰ 0์˜ ์œ„์น˜๋ฅผ ์ฐพ๋Š”๋ฐ js์˜ lastIndexOf๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ”๋กœ ์ฐพ์„ ์ˆ˜ ์žˆ์—ˆ๋‹ค.
  2. ์ •๊ทœํ‘œํ˜„์‹์„ ์ด์šฉํ•ด ํ’€ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค. ๊ธˆ์š”์ผ์— ์‹œ๋„ํ•ด ๋ณผ ์˜ˆ์ •

- ์ฝ”๋“œ

const answer = [];
const solution = (s) => {
  for (let t = 0; t < s.length; t++) {
    let str = s[t];
    let stack = [];
    let target = findTarget(str, stack);
    if (target == "") answer.push(str);
    else {
      const tmpStr = stack.join("");
      const idx = tmpStr.lastIndexOf("0") + 1;
      answer.push(tmpStr.substring(0, idx) + target + tmpStr.substring(idx));
    }
  }
  return answer;
};
const findTarget = (str, stack) => {
  let target = "";
  for (let i = 0; i < str.length; i++) {
    const c = str.charAt(i);
    if (stack.length >= 2) {
      const b = stack.pop();
      const a = stack.pop();
      if (a == "1" && b == "1" && c == "0") {
        target += "110";
        continue;
      }
      stack.push(a);
      stack.push(b);
    }
    stack.push(c);
  }
  return target;
};
profile
์ฐจ๊ทผ์ฐจ๊ทผ ์„ฑ์žฅํ•˜๋Š” ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค

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