๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“šAlgorithm ------------/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ํ•ด์‹œ/์œ„์žฅ/C++

๋ต๊ณก

 

๋ฌธ์ œ ์„ค๋ช…

์ŠคํŒŒ์ด๋“ค์€ ๋งค์ผ ๋‹ค๋ฅธ ์˜ท์„ ์กฐํ•ฉํ•˜์—ฌ ์ž…์–ด ์ž์‹ ์„ ์œ„์žฅํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์ŠคํŒŒ์ด๊ฐ€ ๊ฐ€์ง„ ์˜ท์ด ์•„๋ž˜์™€ ๊ฐ™๊ณ 

์˜ค๋Š˜ ์ŠคํŒŒ์ด๊ฐ€ ๋™๊ทธ๋ž€ ์•ˆ๊ฒฝ, ๊ธด ์ฝ”ํŠธ, ํŒŒ๋ž€์ƒ‰ ํ‹ฐ์…”์ธ ๋ฅผ ์ž…์—ˆ๋‹ค๋ฉด

๋‹ค์Œ๋‚ ์€ ์ฒญ๋ฐ”์ง€๋ฅผ ์ถ”๊ฐ€๋กœ ์ž…๊ฑฐ๋‚˜ ๋™๊ทธ๋ž€ ์•ˆ๊ฒฝ ๋Œ€์‹  ๊ฒ€์ • ์„ ๊ธ€๋ผ์Šค๋ฅผ ์ฐฉ์šฉํ•˜๊ฑฐ๋‚˜ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์ข…๋ฅ˜ ์ด๋ฆ„
์–ผ๊ตด ๋™๊ทธ๋ž€ ์•ˆ๊ฒฝ, ๊ฒ€์ • ์„ ๊ธ€๋ผ์Šค
์ƒ์˜ ํŒŒ๋ž€์ƒ‰ ํ‹ฐ์…”์ธ 
ํ•˜์˜ ์ฒญ๋ฐ”์ง€
๊ฒ‰์˜ท ๊ธด ์ฝ”ํŠธ

์ŠคํŒŒ์ด๊ฐ€ ๊ฐ€์ง„ ์˜์ƒ๋“ค์ด ๋‹ด๊ธด 2์ฐจ์› ๋ฐฐ์—ด clothes๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ

์„œ๋กœ ๋‹ค๋ฅธ ์˜ท์˜ ์กฐํ•ฉ์˜ ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ์‚ฌํ•ญ

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

์ž…์ถœ๋ ฅ ์˜ˆ

 

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

 

 


์ฝ”๋“œ

 

#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>

using namespace std;

int solution(vector<vector<string>> clothes)
{
    int answer = 1;
    unordered_map<string, int> clotheMap;
    for( auto elem : clothes )
    {
        cout << "elem[1]: " << elem[1] << endl;
        clotheMap[elem[1]] += 1;
    }
    for( auto elem: clotheMap)
    {
        answer *= (elem.second+1);
        cout << "answer: " << answer << endl;
    }
    return answer-1;
}

int main(void)
{
    vector<vector<string>> vec;

    vector<string> vec1;
    vec1.push_back("yellowhat");
    vec1.push_back("headgear");

    vector<string> vec2;
    vec2.push_back("bluesunglasses");
    vec2.push_back("eyewear");

    vector<string> vec3;
    vec3.push_back("green_turban");
    vec3.push_back("headgear");

    vec.push_back(vec1);
    vec.push_back(vec2);
    vec.push_back(vec3);

    cout << solution(vec) << endl;

    return 0;
}

 

* warning ํ™•์ธ์ฐจ main ํ•จ์ˆ˜ ์ถ”๊ฐ€ํ•œ๊ฑฐ๋ผ solution ํ•จ์ˆ˜๋งŒ ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค *

 

์š”๊ฑฐ๋„ ํ•ด์‹œ ๋งต์„ ์‚ฌ์šฉํ•ด์„œ ํ’€๋ฉด ๋˜๋Š”๋ฐ...!

๋‚˜๋Š” ๊ธ€์„ ์ž˜ ๋ชป ์ฝ์–ด์„œ ๋ฌธ์ œ ์ดํ•ดํ•˜๋Š”๋ฐ ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค ใ…œใ…œ

์ฑ…์„ ์ฝ์–ด์•ผ ํ•œ๋‹ค ์ด๋ž˜์„œ

 

์ด ๋ฌธ์ œ์˜ ํฌ์ธํŠธ๋Š” ๊ฐ™์€ ์˜์ƒ ์ข…๋ฅ˜์— ๋Œ€ํ•œ ๊ฐฏ์ˆ˜์ด๋‹ค

์˜ˆ๋ฅผ ๋“ค์–ด ์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ์˜ ๊ฒฝ์šฐ headgear ์˜์ƒ์ด ๋‘ ๊ฐ€์ง€(yellowhat๊ณผ green_turban)์ด๋‹ˆ๊นŒ

ํ•ด์‰ฌ ๋งต์— headgear์˜ key๋ฅผ ๊ฐ€์ง€๋Š” value ๊ฐ’์„ ์ฆ๊ฐ€์‹œ์ผœ์ฃผ๋ฉด ๋œ๋‹ค

 

๊ทธ๋Ÿฌ๋ฉด ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ํ†ตํ•ด ์˜์ƒ์„ ๊ณ ๋ฅด๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค

์ฒซ ๋ฒˆ์งธ ์˜ˆ์ œ์˜ ๊ฒฝ์šฐ headgear ๋‘ ๊ฐœ, eyewear ํ•œ ๊ฐœ ์ด๋ฏ€๋กœ

 

headgear ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜ → yellowhat ์ž…์„ ๊ฒฝ์šฐ / green_turban ์ž…์„ ๊ฒฝ์šฐ/ ๋‘˜ ๋‹ค ์•ˆ์ž…์„ ๊ฒฝ์šฐ

eyewear์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜ → bluesunglasses ์ž…์„ ๊ฒฝ์šฐ / ์•ˆ ์ž…์„ ๊ฒฝ์šฐ

 

∴ (์š”์†Œ์˜ ๊ฐœ์ˆ˜ +1)*(์š”์†Œ์˜ ๊ฐœ์ˆ˜ +1) ์ด๋ ‡๊ฒŒ ๋˜๊ณ !

 

๋˜ํ•œ ํ•˜๋ฃจ์— ํ•œ ๊ฐœ์˜ ์˜์ƒ์€ ์ž…์–ด์•ผํ•˜๋‹ˆ๊นŒ

์ „์ฒด ๋‹ค ์•ˆ ์ž…์„ ๊ฒฝ์šฐ์˜ ์ˆ˜์ธ 1์„ ๋นผ๊ธฐ ํ•ด์ฃผ๋ฉด ๊ณต์‹์€ ์™„์„ฑ๋œ๋‹น 

(return answer-1 ํ•ด์ฃผ๋Š” ์ด์œ )