์œ„์žฅ Camouflage

๐Ÿ’ก
Level 2 210919

๋ฌธ์ œ ์„ค๋ช…

์ŠคํŒŒ์ด๋“ค์€ ๋งค์ผ ๋‹ค๋ฅธ ์˜ท์„ ์กฐํ•ฉํ•˜์—ฌ ์ž…์–ด ์ž์‹ ์„ ์œ„์žฅํ•ฉ๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด ์ŠคํŒŒ์ด๊ฐ€ ๊ฐ€์ง„ ์˜ท์ด ์•„๋ž˜์™€ ๊ฐ™๊ณ  ์˜ค๋Š˜ ์ŠคํŒŒ์ด๊ฐ€ ๋™๊ทธ๋ž€ ์•ˆ๊ฒฝ, ๊ธด ์ฝ”ํŠธ, ํŒŒ๋ž€์ƒ‰ ํ‹ฐ์…”์ธ ๋ฅผ ์ž…์—ˆ๋‹ค๋ฉด ๋‹ค์Œ๋‚ ์€ ์ฒญ๋ฐ”์ง€๋ฅผ ์ถ”๊ฐ€๋กœ ์ž…๊ฑฐ๋‚˜ ๋™๊ทธ๋ž€ ์•ˆ๊ฒฝ ๋Œ€์‹  ๊ฒ€์ • ์„ ๊ธ€๋ผ์Šค๋ฅผ ์ฐฉ์šฉํ•˜๊ฑฐ๋‚˜ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์ข…๋ฅ˜
์ด๋ฆ„
๋™๊ทธ๋ž€ ์•ˆ๊ฒฝ, ๊ฒ€์ • ์„ ๊ธ€๋ผ์Šค
ํŒŒ๋ž€์ƒ‰ ํ‹ฐ์…”์ธ 
์ฒญ๋ฐ”์ง€
๊ธด ์ฝ”ํŠธ
์ŠคํŒŒ์ด๊ฐ€ ๊ฐ€์ง„ ์˜์ƒ๋“ค์ด ๋‹ด๊ธด 2์ฐจ์› ๋ฐฐ์—ด clothes๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ์„œ๋กœ ๋‹ค๋ฅธ ์˜ท์˜ ์กฐํ•ฉ์˜ ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • clothes์˜ ๊ฐ ํ–‰์€ [์˜์ƒ์˜ ์ด๋ฆ„, ์˜์ƒ์˜ ์ข…๋ฅ˜]๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ŠคํŒŒ์ด๊ฐ€ ๊ฐ€์ง„ ์˜์ƒ์˜ ์ˆ˜๋Š” 1๊ฐœ ์ด์ƒ 30๊ฐœ ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ๊ฐ™์€ ์ด๋ฆ„์„ ๊ฐ€์ง„ ์˜์ƒ์€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • clothes์˜ ๋ชจ๋“  ์›์†Œ๋Š” ๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 20 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ด๊ณ  ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž ๋˜๋Š” '_' ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ŠคํŒŒ์ด๋Š” ํ•˜๋ฃจ์— ์ตœ์†Œ ํ•œ ๊ฐœ์˜ ์˜์ƒ์€ ์ž…์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

Title
clothes
return
[["yellowhat", "headgear"], ["bluesunglasses", "eyewear"], ["green_turban", "headgear"]]
5
[["crowmask", "face"], ["bluesunglasses", "face"], ["smoky_makeup", "face"]]
3

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์˜ˆ์ œ #1 headgear์— ํ•ด๋‹นํ•˜๋Š” ์˜์ƒ์ด yellow_hat, green_turban์ด๊ณ  eyewear์— ํ•ด๋‹นํ•˜๋Š” ์˜์ƒ์ด blue_sunglasses์ด๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด 5๊ฐœ์˜ ์กฐํ•ฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
1. yellow_hat 2. blue_sunglasses 3. green_turban 4. yellow_hat + blue_sunglasses 5. green_turban + blue_sunglasses
์˜ˆ์ œ #2 face์— ํ•ด๋‹นํ•˜๋Š” ์˜์ƒ์ด crow_mask, blue_sunglasses, smoky_makeup์ด๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด 3๊ฐœ์˜ ์กฐํ•ฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
1. crow_mask 2. blue_sunglasses 3. smoky_makeup
ย 

My solution


์ฒซ ๋ฒˆ์งธ ์ž…์ถœ๋ ฅ ์˜ˆ๋ฅผ ๋ณด๋ฉด headgear์— yellowhat, green_turban 2๊ฐ€์ง€๊ฐ€ ์žˆ๊ณ  eyewear์— bluesunglasses๊ฐ€ ์žˆ๋‹ค.

ํ—ค๋“œ๊ธฐ์–ด๋‚˜ ์•„์ด์ด์–ด๋Š” ๊ฐ๊ฐ ์ฐฉ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ (2+1) * (1+1) = 6์ด ๋˜์–ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.
ํ•˜์ง€๋งŒ ์ œํ•œ์‚ฌํ•ญ์— ๋”ฐ๋ฅด๋ฉด ํ•˜๋ฃจ์— ํ•œ ์˜์ƒ์€ ๋ฐ˜๋“œ์‹œ ์ž…์–ด์•ผ ๋˜๊ธฐ ๋•Œ๋ฌธ์— 6์—์„œ -1์„ ํ•ด์ฃผ๋ฉด ์ตœ์ข… ๋ฆฌํ„ด๊ฐ’์ธ 5๊ฐ€ ๋œ๋‹ค.
[["yellowhat", "headgear"], ["bluesunglasses", "eyewear"], ["green_turban", "headgear"]]
return : 5
ย 
๋‘ ๋ฒˆ์งธ ์ž…์ถœ๋ ฅ ์˜ˆ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์•„์ด์ด์–ด๊ฐ€ 3๊ฐ€์ง€์ธ๋ฐ ์ง๊ด€์ ์œผ๋กœ ๊ณ„์‚ฐ์ด ๋˜์ง€๋งŒ ์œ„์™€ ๊ฐ™์€ ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ (3+1) - 1 = 3์ด ๋œ๋‹ค.
[["crowmask", "face"], ["bluesunglasses", "face"], ["smoky_makeup", "face"]]
return : 3
ย 
clothes๋ฅผ Map์— ์ €์žฅํ•˜๋ฉด์„œ ๊ทธ ์˜์ƒ์˜ ์ข…๋ฅ˜(์ธ๋ฑ์Šค)์— ๊ฐ’์„ 1์”ฉ ์ฆ๊ฐ€์‹œ์ผœ์ค€๋‹ค.
๊ทธ๋‹ค์Œ ๋งŒ๋“ค์–ด์ง„ Map์„ ๋Œ๋ฉด์„œ (์กด์žฌํ•˜๋Š” ๊ฐ’+1)์„ ๊ณฑํ•œ๋’ค ์ตœ์ข… ๊ฐ’์— -1์„ ํ•ด์ฃผ๋ฉด ์กฐํ•ฉ์˜ ์ˆ˜๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ด๋‹ค.
function solution(clothes) {
    var answer = 1;
    let checkMap = new Map();
    for (let [k, v] of clothes) {
        if (checkMap.has(v)) {
            checkMap.set(v, checkMap.get(v) + 1);
        } else checkMap.set(v, 1);
    }

    for (let [k, v] of checkMap) {
        answer *= v + 1;
    }

    return answer - 1;
}
test("solution", () => {
    expect(
        solution([
            ["yellowhat", "headgear"],
            ["bluesunglasses", "eyewear"],
            ["green_turban", "headgear"],
        ])
    ).toBe(5);
    expect(
        solution([
            ["crowmask", "face"],
            ["bluesunglasses", "face"],
            ["smoky_makeup", "face"],
        ])
    ).toBe(3);
});
notion image
ย