๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

42576. ์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜


๋ฌธ์ œ ์œ ํ˜•๋‚œ์ด๋„๊ฑธ๋ฆฐ ์‹œ๊ฐ„ํ•ด๊ฒฐ ์œ ๋ฌด(โœ…/โŒ)
ํ•ด์‹œlv.130๋ถ„โœ…

์„ค๊ณ„ ๋ฐฉ๋ฒ•#

  1. JavaScript์˜ Map ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉ

  2. ๋™๋ช…์ด์ธ์ด ๊ฐ€๋Šฅํ•œ ์™„์ฃผ์ž ์ด๋ฆ„์ด ๋‹ด๊ธด ๋ฐฐ์—ด์„ ๋ณด๊ณ  ์™„์ฃผ์ž ์ด๋ฆ„ : ์ด๋ฆ„์ด ๋‚˜์˜จ ํšŸ์ˆ˜ ์˜ ํ•ด์‰ฌ ๋งต์„ ๋งŒ๋“ฌ

  3. ์ฐธ๊ฐ€์ž ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ, ํ•ด์‰ฌ ๋งต์— ์ฐธ๊ฐ€์ž ์ด๋ฆ„์ด ์žˆ์œผ๋ฉด ์ด๋ฆ„์ด ๋‚˜์˜จ ํšŸ์ˆ˜๋ฅผํ•˜๋‚˜์”ฉ ์ค„์ž„

    • ๋งŒ์•ฝ ์ฐธ๊ฐ€์ž ์ด๋ฆ„์ด ํ•ด์‰ฌ๋งต์— ์—†๊ฑฐ๋‚˜ ์ด๋ฆ„์ด ๋‚˜์˜จ ํšŸ์ˆ˜๊ฐ€ 0 ์ด๋ผ๋ฉด ์ด ์‚ฌ๋žŒ์ด ์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์‚ฌ๋žŒ์ด๋ฏ€๋กœ ์ด ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•จ

์ฝ”๋“œ#

function solution(participants, completions) {    const personCountsByNames = new Map();
    participants.forEach((name) => {        personCountsByNames.set(name, (personCountsByNames.get(name) || 0) + 1);    });
    completions.forEach((name) => {        personCountsByNames.set(name, (personCountsByNames.get(name) || 0) - 1);    });
    for (let [name, personCount] of personCountsByNames) {        if (personCount === 1) {            return name;        }    }}

์‹œ๊ฐ„ ๋ณต์žก๋„#

O(n)

์–ด๋ ค์› ๋˜ ์ #

  • JS์—์„œ ํ•ด์‰ฌ๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ• ์ง€, Map ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•  ์ง€ ๊ณ ๋ฏผํ•จ

  • ์„ฑ๋Šฅ์ƒ Map์ด ์ด์ ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ณ  Map์„ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ•จ

  • Map์€ JS์˜ ๊ฐ์ฒด์™€๋Š” ๋‹ค๋ฅด๊ฒŒ get, set ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์กฐ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋…์„ฑ๋ฉด์—์„œ ์กฐ๊ธˆ ๋–จ์–ด์ง€๋Š” ๋“ฏํ•จ.

  • ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ•œ ์ค„ ์งœ๋ฆฌ ํ’€์ด๋ฅผ ๋ดค๋Š”๋ฐ ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์› ์Œ

var solution = (_, $) =>    _.find(        (_) => !$[_]--,        $.map((_) => ($[_] = ($[_] | 0) + 1)),    );

์ฐธ๊ณ ์ž๋ฃŒ#