ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C++] ์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜
    ์ฝ”๋”ฉํ…Œ์ŠคํŠธ/์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต 2019. 9. 20. 19:46
    728x90
    #include <string>
    #include <vector>
    #include <map>
    #include <iostream>
    
    using namespace std;
    
    /*string solution(vector<string> participant, vector<string> completion) {
    	sort(participant.begin(), participant.end());
    	sort(completion.begin(), completion.end());
    	for (int i = 0; i < completion.size(); i++)
    	{
    		if (participant[i] != completion[i])
    			return participant[i];
    	}
    	return participant[participant.size() - 1];
    }*/
    string solution(vector<string> participant, vector<string> completion) {
    	map<string, int> participants;
    
    	for (string name : participant)
    	{
    		participants[name]++;
    	}
    
    	for (string name : completion)
    	{
    		participants[name]--;
    	}
    
    	for (auto pair : participants)
    	{
    		if (pair.second > 0)
    		{
    			return pair.first;
    		}
    	}
    }
    int main()
    {
    	vector<string> participant;
    	vector<string> completion;
    	string ans;
    
    	participant.push_back("leo");
    	participant.push_back("kiki");
    	participant.push_back("eden");
    	participant.push_back("nikola");
    
    	completion.push_back("leo");
    	completion.push_back("kiki");
    	completion.push_back("nikola");
    
    	ans = solution(participant, completion);
    
    	cout << ans << endl;
    
    }

    ๋ฌธ์ œ ์„ค๋ช…

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

    ๋งˆ๋ผํ†ค์— ์ฐธ์—ฌํ•œ ์„ ์ˆ˜๋“ค์˜ ์ด๋ฆ„์ด ๋‹ด๊ธด ๋ฐฐ์—ด 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๋Š” ์ฐธ์—ฌ์ž ๋ช…๋‹จ์—๋Š” ๋‘ ๋ช…์ด ์žˆ์ง€๋งŒ, ์™„์ฃผ์ž ๋ช…๋‹จ์—๋Š” ํ•œ ๋ช…๋ฐ–์— ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๋ช…์€ ์™„์ฃผํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.

     

     

     

     

    728x90

    ๋Œ“๊ธ€

Designed by Tistory.