[TIL] 211018

Lee SyongΒ·2021λ…„ 10μ›” 18일
0

TIL

λͺ©λ‘ 보기
61/204
post-thumbnail

πŸ“ 였늘 ν•œ 것

  1. ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ 문제 풀이 - μ™„μ£Όν•˜μ§€ λͺ»ν•œ μ„ μˆ˜ / 두 μ •μˆ˜ μ‚¬μ΄μ˜ ν•© / 평균 κ΅¬ν•˜κΈ° / μ§μˆ˜μ™€ ν™€μˆ˜ / 체윑볡

  2. Math.abs() / filter()λ₯Ό μ΄μš©ν•΄ 합집합, ꡐ집합, 차집합 κ΅¬ν•˜λŠ” 법


πŸ“š 배운 것

1. ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ 문제 풀이

1) Level 1 문제 풀이

(1) μ™„μ£Όν•˜μ§€ λͺ»ν•œ μ„ μˆ˜

πŸ”Ž μ˜›λ‚ μ— μž‘μ„±ν–ˆλ˜ 풀이

  • 정확도 ν…ŒμŠ€νŠΈλ§Œ ν†΅κ³Όν•˜κ³  νš¨μœ¨μ„± ν…ŒμŠ€νŠΈλŠ” 0점 λ§žμ•˜λ˜ λ‹΅μ•ˆ...
function solution(participant, completion) {
  for (let i = 0; i < completion.length; i++) { // 순회
    var index = participant.indexOf(completion[i]); // μ„ ν˜• 검색
    participant.splice(index, 1); // μ‚­μ œ
  }
  var answer = participant.toString();

  return answer;
}

πŸ”Ž μˆ˜μ •ν•œ 풀이

  • sort()λ₯Ό μ΄μš©ν•΄ 두 배열을 μ •λ ¬ν•œλ‹€.
  • for 루프λ₯Ό μ΄μš©ν•΄ 각 λ°°μ—΄μ˜ i번째 μš”μ†Œκ°€ μ„œλ‘œ 같은지λ₯Ό ν™•μΈν•œλ‹€.
  • λ‹€λ₯Έ 것을 μ°Ύμ•˜λ‹€λ©΄, κ·Έ μš”μ†Œλ₯Ό λ³€μˆ˜ answer에 ν• λ‹Ήν•΄ λ°˜ν™˜ν•œλ‹€.
function solution(participant, completion) {
  var answer = '';

  participant.sort();
  completion.sort();

  for (let i = 0; i < participant.length; i++) {
    if (participant[i] !== completion[i]) {
      answer = participant[i];
      break;
    }
  }

  return answer;
}

πŸ”Ž λ‹€λ₯Έ μ‚¬λžŒμ˜ 풀이 1

  • pop()을 μ΄μš©ν•΄ 두 λ°°μ—΄μ˜ λμ—μ„œ μ œκ±°ν•œ μš”μ†Œλ“€λΌλ¦¬ λΉ„κ΅ν•œ ν›„, λ‹€λ₯΄λ‹€λ©΄ 이λ₯Ό λ°˜ν™˜ν•œλ‹€.
function solution (participant, completion) {
  participant.sort();
  completion.sort();
  
  while (participant.length) {
    let participant_pop = participant.pop();
    if (participant_pop !== completion.pop()) {
      return participant_pop;
    }
  }
}

πŸ”Ž λ‹€λ₯Έ μ‚¬λžŒμ˜ 풀이 2

forEach()와 ν•΄μ‹œ ν…Œμ΄λΈ”(Hash Table) 이용

  • participant 배열을 ν•΄μ‹œ ν…Œμ΄λΈ”λ‘œ λ§Œλ“ λ‹€
  • p λ°°μ—΄μ˜ μš”μ†Œλ₯Ό key둜 ν•˜κ³ , κ·Έ valueλŠ” 1둜 ν•œλ‹€.
  • λ§Œμ•½ (p λ°°μ—΄μ˜ μš”μ†Œκ°€ μ€‘λ³΅λ˜μ–΄) p λ°°μ—΄μ˜ μš”μ†Œλ₯Ό key둜 ν•˜λŠ” valueκ°€ 이미 μ‘΄μž¬ν•œλ‹€λ©΄, κ·Έ valueλ₯Ό 1 μ¦κ°€μ‹œν‚¨λ‹€.
  • completion λ°°μ—΄μ˜ μš”μ†Œλ₯Ό key둜 ν•˜μ—¬ μ½μ–΄μ˜¨ valueλ₯Ό 1 κ°μ†Œμ‹œν‚¨λ‹€. ( = p λ°°μ—΄κ³Ό c 배열에 μ€‘λ³΅λ˜λŠ” μš”μ†Œλ₯Ό key둜 ν•˜λŠ” valueλ₯Ό 1 κ°μ†Œμ‹œν‚¨λ‹€.)
  • for in 루프λ₯Ό μ΄μš©ν•΄ ν•΄μ‹œ ν…Œμ΄λΈ”μ˜ valueκ°€ 1 이상인 keyλ₯Ό μ°Ύμ•„ μ΅œμ’…μ μœΌλ‘œ κ·Έ keyλ₯Ό λ¦¬ν„΄ν•œλ‹€.
function solution(participant, completion) {
  let hash = [];
  
  participant.forEach(name => {
    hash[name] = hash[name] ? hash[name] + 1 : 1        
  })

  completion.forEach(name => {
    hash[name] = hash[name] - 1
  })

  for (var name in hash) {
    if (hash[name] >= 1) {
      return name
    }
  }
}

(2) 두 μ •μˆ˜ μ‚¬μ΄μ˜ ν•©

πŸ”Ž 처음 μž‘μ„±ν•œ 풀이

  • 정확도 ν…ŒμŠ€νŠΈλŠ” ν†΅κ³Όν–ˆκ³  νš¨μœ¨μ„± ν…ŒμŠ€νŠΈλŠ” μ—†μ—ˆμ§€λ§Œ νš¨μœ¨μ„±μ΄ μ•ˆ 쒋은 풀이닀
function solution(a, b) {
  const nums = [];

  if (a < b) {
    for (let i = a; i <= b; i++) {
      nums.push(i);
    }   
  } else if (a > b) {
    for (let i = b; i <= a; i++) {
      nums.push(i);
    }   
  } else {
    return a;
  }

  return nums.reduce((prev, curr) => prev + curr, 0);
}

πŸ”Ž μˆ˜μ •ν•œ 풀이

  • Math.min()κ³Ό Math.max() μ‚¬μš©
  • reduce() λŒ€μ‹  sum λ³€μˆ˜λ₯Ό 이용
function solution(a, b) {
  if (a === b) {
    return a;
  }

  let sum = Math.min(a, b);    
  for (let i = Math.min(a, b) + 1; i <= Math.max(a, b); i++) {
    sum = sum + i;
  }
  return sum;
}

πŸ”Ž λ‹€λ₯Έ μ‚¬λžŒμ˜ 풀이

  • Math.abs() 이용

    주어진 숫자의 μ ˆλŒ€κ°’μ„ λ°˜ν™˜ν•œλ‹€
    ex) console.log(Math.abs(4 - 9)); // 5

function solution(a, b) {
  return (a + b) * (Math.abs(b - a) + 1) / 2;
}

(3) 평균 κ΅¬ν•˜κΈ°

πŸ”Ž λ‚΄κ°€ μž‘μ„±ν•œ 풀이

function solution(arr) {
  return arr.reduce((prev, curr) => prev + curr, 0) / arr.length;
}

(4) μ§μˆ˜μ™€ ν™€μˆ˜

πŸ”Ž λ‚΄κ°€ μž‘μ„±ν•œ 풀이

function solution(num) {    
  if (num % 2 === 0) {
    return 'Even';
  } else {
    return 'Odd';
  }
}

πŸ”Ž λ‹€λ₯Έ μ‚¬λžŒμ˜ 풀이

  • 쑰건뢀 μ‚Όν•­ μ—°μ‚°μž 이용
function solution(num) {    
  return num % 2 === 0 ? 'Even' : 'Odd';
}

(5) 체윑볡

πŸ”Ž 처음 μž‘μ„±ν•œ 풀이

  • 각각 lost와 reserveμ—μ„œ 체윑볡 μ—¬λ²Œμ„ 가지고 μžˆμœΌλ©΄μ„œ λ™μ‹œμ— ν•œ λ²Œμ„ λ„λ‚œλ‹Ήν•œ 학생듀 즉, lost와 reserve의 ꡐ집합을 μ œκ±°ν•œ ν›„, lost와 reserve 배열에 λ‹΄κΈ΄ μš”μ†Œ(학생듀 번호)듀을 쑰건을 κ³ λ €ν•΄ λΉ„κ΅ν•˜κ³ μž ν–ˆλ‹€.

  • ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€λŠ” ν†΅κ³Όν–ˆμœΌλ‚˜, μ‹€μ œ 채점 μ‹œ 정확도 ν…ŒμŠ€νŠΈμ—μ„œ 55점 λ§žμ€ λ‹΅μ•ˆ...μ΄μ—ˆλŠ”λ° λ‹€μ‹œ ν•˜λ‹ˆκΉŒ 85점 λ‚˜μ˜¨λ‹€. 뭐지? μ–΄μ¨Œλ“  ν‹€λ¦° 풀이닀.

function solution(n, lost, reserve) {
  var answer = 0;

  const lostCount = lost.length;
  const reserveCount = reserve.length;

  const intersection = [];

  for (let i = 0; i < lost.length; i++) {
    for (let j = 0; j < reserve.length; j++) {
      if (lost[i] === reserve[j]) {
        intersection.push(lost[i]);
        lost.splice(i, 1);
        reserve.splice(j, 1);
        break;
      }
    }
  }

  
  let lentCount = 0;

  for (let i = 0; i < reserve.length; i++) {
    for (let j = 0; j < lost.length; j++) {
      if (reserve[i] === lost[j] - 1 || reserve[i] === lost[j] + 1) {
        lentCount++;
        lost.splice(j , 1);
        break;
      }
    }
  }

  const rest = n - (lostCount + reserveCount - intersection.length);
  answer = rest + reserveCount + lentCount;
  return answer;
}

πŸ”Ž 두 번째 μž‘μ„±ν•œ 풀이

  • ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€λ₯Ό ν•˜λ‚˜μ”© μΆ”κ°€ν•΄μ„œ 였λ₯˜λ₯Ό μ°Ύμ•˜λ‹€. ꡐ집합을 κ΅¬ν•˜λŠ” for λ£¨ν”„μ—μ„œ spliceλ₯Ό μ΄μš©ν•΄ lost λ°°μ—΄μ˜ 쀑간 μš”μ†Œλ₯Ό 계속 μ œκ±°ν•˜λŠ” 뢀뢄이 λ¬Έμ œμ˜€λ‹€. λ°”κΉ₯ 루프가 λ‹€μŒ 루프λ₯Ό 돌 λ•Œ iλŠ” 이전에 λΉ„ν•΄ λ˜‘κ°™μ΄ 1이 μ¦κ°€ν–ˆμ§€λ§Œ, μ‹€μ œ lost λ°°μ—΄μ—μ„œλŠ” 이전 μš”μ†Œμ˜ λ‹€μŒ μš”μ†Œκ°€ μ•„λ‹ˆλΌ λ‹€λ‹€μŒ μš”μ†Œκ°€ 작히게 λœλ‹€. μ΄λŠ” 이전 λ£¨ν”„μ—μ„œ splice에 μ˜ν•΄ κ·Έ λ£¨ν”„μ—μ„œ μž‘ν˜”λ˜ μš”μ†Œλ₯Ό μ œκ±°ν–ˆκΈ° λ•Œλ¬Έμ΄λ‹€.

  • λ˜ν•œ, splice둜 인해 lost λ°°μ—΄κ³Ό reserve 배열을 μ œκ±°ν–ˆμ–΄λ„ lentCountλ₯Ό μ¦κ°€μ‹œν‚€λŠ” 쀑첩 λ£¨ν”„μ—μ„œ μ‚¬μš©λ˜λŠ” lost λ°°μ—΄κ³Ό reserve 배열은 λ‹Ήμ—°ν•˜κ²Œλ„ solution ν•¨μˆ˜μ˜ λ§€κ°œλ³€μˆ˜λ‘œ μ „λ‹¬λœ λ°°μ—΄λ“€ κ·ΈλŒ€λ‘œμ˜€κΈ° λ•Œλ¬Έμ— μ˜λ„ν–ˆλ˜ λŒ€λ‘œ 비ꡐ가 λ˜μ§€ μ•Šμ•˜λ‹€.

  • 각각 lost λ°°μ—΄κ³Ό reserve λ°°μ—΄μ—μ„œ ꡐ집합을 μ œκ±°ν•˜λŠ” 데 쀑첩 루프λ₯Ό μ‚¬μš©ν•˜λ‹ˆκΉŒ 블둝 μŠ€μ½”ν”„ μ•ˆμ—μ„œλ§Œ λ³€ν™”κ°€ μ μš©λΌμ„œ λ‹€μŒ μ½”λ“œμ—μ„œ μ΄μ–΄μ„œ μ“Έ μˆ˜κ°€ μ—†μ—ˆλ‹€. λ‹€λ₯Έ 방법이 μžˆλ‚˜ 검색해보닀 신세계 발견. [javascript] λ°°μ—΄ κ°’ μ€‘λ³΅μ œκ±°, λ°°μ—΄ν•©μΉ˜κΈ° μ°Έκ³ 

  • λ§žμ„ 쀄 μ•Œμ•˜λŠ”λ° 90점이 λ‚˜μ™”λ‹€. 뭐가 문제일까.

function solution(n, lost, reserve) {
  var answer = 0;

  const lostCount = lost.length;
  const reserveCount = reserve.length;

  // lost + reserve
  const sum = lost.concat(reserve);

  // ꡐ집합
  const intersection = sum.filter((item, index) => sum.indexOf(item) !== index);

  // 차집합 : lost - (lost ∩ reserve)
  const restLost = lost.filter(x => !intersection.includes(x));

  // 차집합 : reserve - (lost ∩ reserve)
  const restReserve = reserve.filter(x => !intersection.includes(x));


  // μ²΄μœ‘λ³΅μ„ 빌린 학생 수
  let lentCount = 0;

  for (let i = 0; i < restReserve.length; i++) {
    for (let j = 0; j < restLost.length; j++) {
      if (restReserve[i] === restLost[j] - 1 || restReserve[i] === restLost[j] + 1) {
        lentCount++;
        restLost.splice(j , 1);
        break;
      }
    }
  }


  // 본인 체윑볡만 κ°€μ Έμ™€μ„œ 본인이 μž…λŠ” 학생 수 (λ„λ‚œx, 여별x)
  const rest = n - (lostCount + reserveCount - intersection.length);

  // '전체 학생 수 n - μ²΄μœ‘λ³΅μ„ λͺ» 빌린 학생 수'와 κ°™λ‹€
  answer = rest + reserveCount + lentCount;

  return answer;
}

πŸ”Ž μ„Έ 번째 μž‘μ„±ν•œ 풀이

  • 맨 μ²˜μŒμ— lost λ°°μ—΄κ³Ό reserve 배열을 μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬ν•΄μ£ΌλŠ” μ½”λ“œλ₯Ό μΆ”κ°€ν–ˆλ‹€.

  • λ“œλ””μ–΄ 100점이닀.

function solution(n, lost, reserve) {
  lost.sort((a, b) => a - b);
  reserve.sort((a, b) => a - b);
  
  const sum = lost.concat(reserve);
  const intersection = sum.filter((item, index) => sum.indexOf(item) !== index);
  const restLost = lost.filter(x => !intersection.includes(x));
  const restReserve = reserve.filter(x => !intersection.includes(x));

  const lostCount = lost.length;
  const reserveCount = reserve.length;
  let lentCount = 0;

  for (let i = 0; i < restReserve.length; i++) {
    for (let j = 0; j < restLost.length; j++) {
      if (restReserve[i] === restLost[j] - 1 || restReserve[i] === restLost[j] + 1) {
        lentCount++;
        restLost.splice(j , 1);
        break;
      }
    }
  }

  const rest = n - (lostCount + reserveCount - intersection.length);
  return rest + reserveCount + lentCount;
}

✨ 내일 ν•  것

  1. ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ 문제 풀이
profile
λŠ₯λ™μ μœΌλ‘œ μ‚΄μž, ν–‰λ³΅ν•˜κ²ŒπŸ˜

0개의 λŒ“κΈ€