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

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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ํ•ด์‹œ/์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜/C++

๋ฌด๋„๋Ÿฌ๋ฒ„๋“ค๋งŒ ์•Œ์•„๋ณผ ์งค

 

๋ฌธ์ œ ์„ค๋ช…

์ˆ˜๋งŽ์€ ๋งˆ๋ผํ†ค ์„ ์ˆ˜๋“ค์ด ๋งˆ๋ผํ†ค์— ์ฐธ์—ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋‹จ ํ•œ ๋ช…์˜ ์„ ์ˆ˜๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ๋ชจ๋“  ์„ ์ˆ˜๊ฐ€ ๋งˆ๋ผํ†ค์„ ์™„์ฃผํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๋งˆ๋ผํ†ค์— ์ฐธ์—ฌํ•œ ์„ ์ˆ˜๋“ค์˜ ์ด๋ฆ„์ด ๋‹ด๊ธด ๋ฐฐ์—ด participant์™€ ์™„์ฃผํ•œ ์„ ์ˆ˜๋“ค์˜ ์ด๋ฆ„์ด ๋‹ด๊ธด ๋ฐฐ์—ด completion์ด ์ฃผ์–ด์งˆ ๋•Œ, ์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜์˜ ์ด๋ฆ„์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ์‚ฌํ•ญ

  • ๋งˆ๋ผํ†ค ๊ฒฝ๊ธฐ์— ์ฐธ์—ฌํ•œ ์„ ์ˆ˜์˜ ์ˆ˜๋Š” 1๋ช… ์ด์ƒ 100,000๋ช… ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • completion์˜ ๊ธธ์ด๋Š” participant์˜ ๊ธธ์ด๋ณด๋‹ค 1 ์ž‘์Šต๋‹ˆ๋‹ค.
  • ์ฐธ๊ฐ€์ž์˜ ์ด๋ฆ„์€ 1๊ฐœ ์ด์ƒ 20๊ฐœ ์ดํ•˜์˜ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ฐธ๊ฐ€์ž ์ค‘์—๋Š” ๋™๋ช…์ด์ธ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

  • ์ž…์ถœ๋ ฅ ์˜ˆ
participant completion return
["leo", "kiki", "eden"] ["eden", "kiki"] "leo"
["marina", "josipa", "nikola", "vinko", "filipa"] ["josipa", "filipa", "marina", "nikola"] "vinko"
["mislav", "stanko", "mislav", "ana"] ["stanko", "ana", "mislav"] "mislav"

 

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

์˜ˆ์ œ #1
"leo"๋Š” ์ฐธ์—ฌ์ž ๋ช…๋‹จ์—๋Š” ์žˆ์ง€๋งŒ, ์™„์ฃผ์ž ๋ช…๋‹จ์—๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์— ์™„์ฃผํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ #2
"vinko"๋Š” ์ฐธ์—ฌ์ž ๋ช…๋‹จ์—๋Š” ์žˆ์ง€๋งŒ, ์™„์ฃผ์ž ๋ช…๋‹จ์—๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์— ์™„์ฃผํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ #3
"mislav"๋Š” ์ฐธ์—ฌ์ž ๋ช…๋‹จ์—๋Š” ๋‘ ๋ช…์ด ์žˆ์ง€๋งŒ, ์™„์ฃผ์ž ๋ช…๋‹จ์—๋Š” ํ•œ ๋ช…๋ฐ–์— ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๋ช…์€ ์™„์ฃผํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.

 

#include <string>
#include <vector>
#include <map>

using namespace std;

string solution(vector<string> participant, vector<string> completion) {
    string answer = "";
    if( participant.empty() || participant.size() > 100000 )
        return answer;
    
    if(  (participant.size() -1) != completion.size() )
        return answer;
    
    map<string, int> ansMap;
    for( auto cp : completion )
    {
        ansMap[cp] += 1;
    }
    for( auto pc : participant )
    {
        ansMap[pc] -= 1;
        if(  ansMap[pc] < 0)
            return pc;
    }
    
    return answer;
}

 

์ฒ˜์Œ์— algorithm ํ—ค๋” ์จ์„œ find-if๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค๊ฐ€

์‹œ๊ฐ„ ์˜ค๋ž˜๊ฑธ๋ฆฐ๋Œ€์„œ ์ทจ์†Œ ๐Ÿ˜‚

 

map์˜ value ๊ฐ’์„ bool ๊ฐ’์œผ๋กœ ํ•˜๊ณ  ์‹ถ์—ˆ๋Š”๋ฐ (์ฒดํฌ ์˜๋ฏธ๋กœ)

๋‚ด๊ฐ€ ํ—ท๊ฐˆ๋ ค์„œ ๊ทธ๋ƒฅ int ํ˜•์œผ๋กœ ๋ฐ”๊ฟจ๋•…...

 

๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด์— ์ข‹์€ ๋‹ต์ด ์ฐธ ๋งŽ์•˜๋‹ค

๋˜ ํ•œ๋ฒˆ ๋ฐ˜์„ฑํ•˜๊ณ  ๊ฐ‘๋‹ˆ๋‹ค