๐Ÿ… ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 152995 : ์ธ์‚ฌ๊ณ ๊ณผ - Javascript

๋ฌธ์ œ ์„ค๋ช…

์™„ํ˜ธ๋„ค ํšŒ์‚ฌ๋Š” ์—ฐ๋ง๋งˆ๋‹ค 1๋…„ ๊ฐ„์˜ ์ธ์‚ฌ๊ณ ๊ณผ์— ๋”ฐ๋ผ ์ธ์„ผํ‹ฐ๋ธŒ๋ฅผ ์ง€๊ธ‰ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์‚ฌ์›๋งˆ๋‹ค ๊ทผ๋ฌด ํƒœ๋„ ์ ์ˆ˜์™€ ๋™๋ฃŒ ํ‰๊ฐ€ ์ ์ˆ˜๊ฐ€ ๊ธฐ๋ก๋˜์–ด ์žˆ๋Š”๋ฐ ๋งŒ์•ฝ ์–ด๋–ค ์‚ฌ์›์ด ๋‹ค๋ฅธ ์ž„์˜์˜ ์‚ฌ์›๋ณด๋‹ค ๋‘ ์ ์ˆ˜๊ฐ€ ๋ชจ๋‘ ๋‚ฎ์€ ๊ฒฝ์šฐ๊ฐ€ ํ•œ ๋ฒˆ์ด๋ผ๋„ ์žˆ๋‹ค๋ฉด ๊ทธ ์‚ฌ์›์€ ์ธ์„ผํ‹ฐ๋ธŒ๋ฅผ ๋ฐ›์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์€ ์‚ฌ์›๋“ค์— ๋Œ€ํ•ด์„œ๋Š” ๋‘ ์ ์ˆ˜์˜ ํ•ฉ์ด ๋†’์€ ์ˆœ์œผ๋กœ ์„์ฐจ๋ฅผ ๋‚ด์–ด ์„์ฐจ์— ๋”ฐ๋ผ ์ธ์„ผํ‹ฐ๋ธŒ๊ฐ€ ์ฐจ๋“ฑ ์ง€๊ธ‰๋ฉ๋‹ˆ๋‹ค.

์ด๋•Œ, ๋‘ ์ ์ˆ˜์˜ ํ•ฉ์ด ๋™์ผํ•œ ์‚ฌ์›๋“ค์€ ๋™์„์ฐจ์ด๋ฉฐ, ๋™์„์ฐจ์˜ ์ˆ˜๋งŒํผ ๋‹ค์Œ ์„์ฐจ๋Š” ๊ฑด๋„ˆ ๋œ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ ์ˆ˜์˜ ํ•ฉ์ด ๊ฐ€์žฅ ํฐ ์‚ฌ์›์ด 2๋ช…์ด๋ผ๋ฉด 1๋“ฑ์ด 2๋ช…์ด๊ณ  2๋“ฑ ์—†์ด ๋‹ค์Œ ์„์ฐจ๋Š” 3๋“ฑ๋ถ€ํ„ฐ์ž…๋‹ˆ๋‹ค.

๊ฐ ์‚ฌ์›์˜ ๊ทผ๋ฌด ํƒœ๋„ ์ ์ˆ˜์™€ ๋™๋ฃŒ ํ‰๊ฐ€ ์ ์ˆ˜ ๋ชฉ๋ก scores์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์™„ํ˜ธ์˜ ์„์ฐจ๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.


์ œํ•œ์‚ฌํ•ญ

  • 1 โ‰ค scores์˜ ๊ธธ์ด โ‰ค 100,000
  • scores์˜ ๊ฐ ํ–‰์€ ํ•œ ์‚ฌ์›์˜ ๊ทผ๋ฌด ํƒœ๋„ ์ ์ˆ˜์™€ ๋™๋ฃŒ ํ‰๊ฐ€ ์ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ [a, b] ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค.
    • scores[0]์€ ์™„ํ˜ธ์˜ ์ ์ˆ˜์ž…๋‹ˆ๋‹ค.
    • 0 โ‰ค a, b โ‰ค 100,000
  • ์™„ํ˜ธ๊ฐ€ ์ธ์„ผํ‹ฐ๋ธŒ๋ฅผ ๋ฐ›์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ -1์„ return ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

scores result
[[2,2],[1,4],[3,2],[3,2],[2,1]] 4
  • 5 ๋ฒˆ์งธ ์‚ฌ์›์€ 3 ๋ฒˆ์งธ ๋˜๋Š” 4 ๋ฒˆ์งธ ์‚ฌ์›๋ณด๋‹ค ๊ทผ๋ฌด ํƒœ๋„ ์ ์ˆ˜์™€ ๋™๋ฃŒ ํ‰๊ฐ€ ์ ์ˆ˜๊ฐ€ ๋ชจ๋‘ ๋‚ฎ๊ธฐ ๋•Œ๋ฌธ์— ์ธ์„ผํ‹ฐ๋ธŒ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

  • 2 ๋ฒˆ์งธ ์‚ฌ์›, 3 ๋ฒˆ์งธ ์‚ฌ์›, 4 ๋ฒˆ์งธ ์‚ฌ์›์€ ๋‘ ์ ์ˆ˜์˜ ํ•ฉ์ด 5 ์ ์œผ๋กœ ์ตœ๊ณ ์ ์ด๋ฏ€๋กœ 1 ๋“ฑ์ž…๋‹ˆ๋‹ค.

  • 1 ๋“ฑ์ด ์„ธ ๋ช…์ด๋ฏ€๋กœ 2 ๋“ฑ๊ณผ 3 ๋“ฑ์€ ์—†๊ณ  1 ๋ฒˆ์งธ ์‚ฌ์›์ธ ์™„ํ˜ธ๋Š” ๋‘ ์ ์ˆ˜์˜ ํ•ฉ์ด 4 ์ ์œผ๋กœ 4 ๋“ฑ์ž…๋‹ˆ๋‹ค.


ํ’€์ด ๊ณผ์ •

์ „์ฒด ์„ ์ˆ˜(participant) ์ค‘ ์™„์ฃผํ•œ ์„ ์ˆ˜(completion) ๋ชฉ๋ก์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์„ ์ˆ˜ ํ•œ ๋ช…์„ ์ฐพ์•„๋‚ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


์‹œ๋„ 1

splice์™€ indexOf๋ฅผ ์ด์šฉํ•ด ์™„์ฃผํ•œ ์„ ์ˆ˜(completion)๋ฅผ ์ „์ฒด ์„ ์ˆ˜(participant)์—์„œ ํ•˜๋‚˜์”ฉ ์†Œ๊ฑฐํ•œ ๋’ค, ๋‚จ์€ ํ•œ ์„ ์ˆ˜์˜ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค.

function solution(scores) {
  const len = scores.length;
  for(let i = 1; i < len; ++i){
    if(scores[0][1] < scores[i][0] && scores[0][1] < scores[i][1]){
      return -1;
    }
  }

  const totalScores = scores.map([a,b] => a + b);
}

ํ•˜์ง€๋งŒ ์ •ํ™•๋„ ํ…Œ์ŠคํŠธ๋Š” ํ†ต๊ณผํ•˜๊ณ  ์‹œ๊ฐ„ ์ดˆ๊ณผ๋กœ ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ๋Š” ํ†ต๊ณผํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฐฐ์—ด์„ ์ˆ˜์ •ํ•˜๋Š” splice๊ฐ€ ๋Š๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๊ธด ๋ฌธ์ œ์ธ ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค.


์‹œ๋„ 2

์•„๋ž˜์™€ ๊ฐ™์ด ํ•ด์‰ฌ๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ํ’€์—ˆ๊ณ , ์„ฑ๊ณต์ ์œผ๋กœ ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ๊นŒ์ง€ ํ†ต๊ณผํ–ˆ์Šต๋‹ˆ๋‹ค.

function solution(participant, completion) {
  // ์™„์ฃผํ•œ ์„ ์ˆ˜์˜ ์ด๋ฆ„์˜ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜ ์„ ์–ธ
  const counts = {};

  // ์™„์ฃผํ•œ ์„ ์ˆ˜์˜ ์ด๋ฆ„์˜ ๊ฐœ์ˆ˜๋ฅผ `counts` ๋ณ€์ˆ˜์— ๋‹ด๊ธฐ
  for (name of completion) {
    if (counts[name]) {
      counts[name]++;
    } else {
      counts[name] = 1;
    }
  }

  for (name of participant) {
    if (counts[name]) {
      counts[name]--; // ์„ ์ˆ˜ ์ด๋ฆ„์ด ์žˆ์œผ๋ฉด ๊ฐœ์ˆ˜ 1 ๊ฐ์†Œ
    } else {
      return name; // ์„ ์ˆ˜ ์ด๋ฆ„์ด ์—†๊ฑฐ๋‚˜ 0์ด๋ฉด ํ•ด๋‹น ์„ ์ˆ˜์˜ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜
    }
  }
}

์ˆ์ฝ”๋”ฉ (CodeGolf)

์กฐ๊ธˆ ๋” ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด ์กฐ๊ฑด์‹ ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•ด ์•„๋ž˜์™€ ๊ฐ™์ด ์ •๋ฆฌ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

function solution(participant, completion) {
  const counts = {};
  for (name of completion) counts[name] = (counts[name] | 0) + 1;
  for (name of participant) if (!counts[name]--) return name;
}
counts์— name์ด ์—†์„ ์ˆ˜๋„ ์žˆ๋Š”๋ฐ !counts[name]--์ด ์™œ ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜๋‚˜์š”?
  1. counts[name]๊ฐ€ ๋น„์–ด์žˆ์„ ๊ฒฝ์šฐ ์ ‘๊ทผํ•˜๋ฉด undefined๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  2. undefined๊ฐ€ ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž์™€ ๋งŒ๋‚˜๋ฉด ์˜ค๋ฅ˜ ์—†์ด NaN์œผ๋กœ ๋ฐ”๋€Œ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  3. NaN์€ ๋…ผ๋ฆฌ๊ฐ’ ๊ฑฐ์ง“์œผ๋กœ ๋ณ€ํ™˜๋˜๊ธฐ ๋•Œ๋ฌธ์—, !NaN์€ true๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ counts[name]์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ๋•Œ, !counts[name]--๋Š” true์œผ๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ๊ณผ์ •์„ ํ†ตํ•ด ๋งคํ•‘๋œ ๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ์—๋„ ์„ ์ˆ˜์˜ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋กœ์ง์ด ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


์ถ”๊ฐ€๋กœ counts ๊ฐ์ฒด ์ƒ์„ฑ ๋ผ์ธ์„ ์ œ๊ฑฐํ•˜๊ณ  completion ๋ฐฐ์—ด์„ ํ•ด์‰ฌ๋งต ์šฉ๋„๋กœ ์žฌ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

function solution(participant, completion) {
  for (name of completion) completion[name] = (completion[name] | 0) + 1;
  for (name of participant) if (!completion[name]--) return name;
}
completion๋Š” ๋ฐฐ์—ด์ธ๋ฐ ์–ด๋–ป๊ฒŒ ํ‚ค๊ฐ’์„ ์ธ๋ฑ์Šค๋กœ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‚˜์š”?
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ๋ฐฐ์—ด ๋˜ํ•œ Array๋ผ๋Š” ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— ์†์„ฑ(property)์„ ์ถ”๊ฐ€ํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ completion[name]์ด๋ผ๋Š” ์ฝ”๋“œ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

for-of๋ฅผ map()๊ณผ find() ๋ฉ”์†Œ๋“œ๋กœ ๋ฐ”๊พธ๊ณ  ํ•œ์ค„์˜ ์ฝ”๋“œ๋กœ ํ•ฉ์นฉ๋‹ˆ๋‹ค.

function solution(participant, completion) {
  completion.map((name) => (completion[name] = (completion[name] | 0) + 1));
  return participant.find((name) => !completion[name]--, completion);
}
solution = (participant, completion) =>
  participant.find(
    (name) => !completion[name]--,
    completion.map((name) => (completion[name] = (completion[name] | 0) + 1))
  );

๋งˆ์ง€๋ง‰์œผ๋กœ ๊ฐ€๋…์„ฑ์„ ํฌ๊ธฐํ•˜๊ณ  ๋ชจ๋“  ์ด๋ฆ„์„ ํ•œ๊ธ€์ž๋กœ ์ˆ˜์ •ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ์ฝ”๋”ฉ ์ฝ”๋“œ๊ฐ€ ์™„์„ฑ๋ฉ๋‹ˆ๋‹ค.

solution = (p, c) =>
  p.find(
    (n) => !c[n]--,
    c.map((n) => (c[n] = (c[n] | 0) + 1))
  );