Notice
Recent Posts
Recent Comments
Today
Total
01-10 18:14
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์œ„์žฅ ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์œ„์žฅ

WonderJay 2022. 7. 13. 12:16
728x90
๋ฐ˜์‘ํ˜•
SMALL

https://school.programmers.co.kr/learn/courses/30/lessons/42578

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

์˜ท์˜ ์ข…๋ฅ˜์™€ ์ด๋ฆ„์ด pair ๋กœ ๋‹ด๊ธด clothes ๋ฐฐ์—ด์ด ์ธํ’‹์œผ๋กœ ๋“ค์–ด์˜ค๋ฉด, ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ์ˆ˜์˜ ์˜ท์„ ์ž…์„ ์ˆ˜ ์žˆ๋Š” ์ง€ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•ด์•ผ ํ•œ๋‹ค. ๋‹น์—ฐํ•˜์ง€๋งŒ ๋™์ผ ์ข…๋ฅ˜์˜ ์˜ท์€ ํ•˜๋‚˜๋งŒ ์„ ํƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, [["yellow_hat", "headgear"], ["blue_sunglasses", "eyewear"], ["green_turban", "headgear"]]  ์™€ ๊ฐ™์ด ์ž…๋ ฅ์ด ๋“ค์–ด์™”๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด๋ฉด, headgear 2๊ฐœ, eyewear 1๊ฐœ๊ฐ€ ๋“ค์–ด์˜จ ๊ฒƒ์ด๋‹ค. ์ด๋ฅผ ์ด์šฉํ•ด์„œ ์ฐฉ์šฉ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š”  (2+1)(1+1) - 1 = 6 - 1 = 5 ์™€ ๊ฐ™๋‹ค.

headgear ๋Š” ์ด 2๊ฐœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ, ํ•˜๋‚˜๋„ ์„ ํƒํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์œผ๋ฏ€๋กœ headgear ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” 2+1 = 3 ๊ฐœ์ธ ๊ฒƒ์ด๊ณ ,

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ eyewear ๋Š” ์ด 1๊ฐœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ ํ•˜๋‚˜๋„ ์„ ํƒํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์œผ๋ฏ€๋กœ eyewear ๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” 1+1 = 2 ์ธ ๊ฒƒ์ด๋‹ค.

๊ทธ๋ž˜์„œ [["yellow_hat", "headgear"], ["blue_sunglasses", "eyewear"], ["green_turban", "headgear"]]  ์™€ ๊ฐ™์ด ์ž…๋ ฅ์ด ๋“ค์–ด์˜จ ๊ฒฝ์šฐ์—๋Š” 2*3 = 6 ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š”๋ฐ, ์ด ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” ๋‹จ ํ•˜๋‚˜๋„ ์ฐฉ์šฉํ•˜์ง€ ์•Š์€ ์ƒํ™ฉ ๋˜ํ•œ ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ 1 ์„ ๋นผ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

์ด๋ฅผ ์ผ๋ฐ˜ํ™” ํ•˜๋ฉด ํ•ด๋‹น ๋ฌธ์ œ์˜ ๋‹ต์•ˆ์€ (a+1)(b+1)(c+1)...(z+1) - 1 ๊ณผ ๊ฐ™๋‹ค.  (a, b, c, ... , z ๋Š” ๊ฐ๊ฐ์˜ ์˜ท ์ข…๋ฅ˜์— ๋Œ€ํ•œ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์˜ท์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค.) 

 

์—ฌํŠผ ์œ„ ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•˜๋ ค๋ฉด ๊ฐ๊ฐ์˜ ์ข…๋ฅ˜์— ๋Œ€ํ•ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์˜ท์˜ ๊ฐœ์ˆ˜๋ฅผ ์•Œ์•„๋‚ด์•ผํ•œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ <์˜ท์˜ ์ข…๋ฅ˜, ๊ฐœ์ˆ˜> ๋ฅผ pair ๋กœ ๊ฐ€์ง€๋Š” map ์ธ closet ์„ ์„ ์–ธํ•˜์˜€๋‹ค. ๊ทธ๋ฆฌ๊ณ  clothes ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ํ•ด๋‹นํ•˜๋Š” ์˜ท ์ข…๋ฅ˜์— ๋Œ€ํ•œ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŒ…ํ•˜์˜€๊ณ , closet ์„ ์ˆœํšŒํ•˜๋ฉฐ ์œ„ ๊ณต์‹์„ ์ด์šฉํ•ด์„œ ์ตœ์ข… ๋‹ต์•ˆ์„ ๋„์ถœํ•œ๋‹ค.

 

[C++]

#include <string>
#include <vector>
#include <map>
#include <set>
#include <iostream>
#include <algorithm>

using namespace std;

int solution(vector<vector<string>> clothes) {
    int answer = 1;
    
    map<string, int> closet;
    
    for(int i = 0 ; i < clothes.size(); i ++)
    {
        closet[clothes[i][1]]++;
    }
    
    for(auto& ele : closet){
        answer *= (ele.second+1);
    }   
    answer = answer - 1;
    
    return answer;
}

 

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments