ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C++] ๋ฒ ์ŠคํŠธ์•จ๋ฒ”
    ์ฝ”๋”ฉํ…Œ์ŠคํŠธ/์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต 2019. 9. 21. 01:46
    728x90

    ๋ฌธ์ œ ์„ค๋ช…

    ์ŠคํŠธ๋ฆฌ๋ฐ ์‚ฌ์ดํŠธ์—์„œ ์žฅ๋ฅด ๋ณ„๋กœ ๊ฐ€์žฅ ๋งŽ์ด ์žฌ์ƒ๋œ ๋…ธ๋ž˜๋ฅผ ๋‘ ๊ฐœ์”ฉ ๋ชจ์•„ ๋ฒ ์ŠคํŠธ ์•จ๋ฒ”์„ ์ถœ์‹œํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๋…ธ๋ž˜๋Š” ๊ณ ์œ  ๋ฒˆํ˜ธ๋กœ ๊ตฌ๋ถ„ํ•˜๋ฉฐ, ๋…ธ๋ž˜๋ฅผ ์ˆ˜๋กํ•˜๋Š” ๊ธฐ์ค€์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    1. ์†ํ•œ ๋…ธ๋ž˜๊ฐ€ ๋งŽ์ด ์žฌ์ƒ๋œ ์žฅ๋ฅด๋ฅผ ๋จผ์ € ์ˆ˜๋กํ•ฉ๋‹ˆ๋‹ค.
    2. ์žฅ๋ฅด ๋‚ด์—์„œ ๋งŽ์ด ์žฌ์ƒ๋œ ๋…ธ๋ž˜๋ฅผ ๋จผ์ € ์ˆ˜๋กํ•ฉ๋‹ˆ๋‹ค.
    3. ์žฅ๋ฅด ๋‚ด์—์„œ ์žฌ์ƒ ํšŸ์ˆ˜๊ฐ€ ๊ฐ™์€ ๋…ธ๋ž˜ ์ค‘์—์„œ๋Š” ๊ณ ์œ  ๋ฒˆํ˜ธ๊ฐ€ ๋‚ฎ์€ ๋…ธ๋ž˜๋ฅผ ๋จผ์ € ์ˆ˜๋กํ•ฉ๋‹ˆ๋‹ค.

    ๋…ธ๋ž˜์˜ ์žฅ๋ฅด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด genres์™€ ๋…ธ๋ž˜๋ณ„ ์žฌ์ƒ ํšŸ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ ๋ฐฐ์—ด plays๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ๋ฒ ์ŠคํŠธ ์•จ๋ฒ”์— ๋“ค์–ด๊ฐˆ ๋…ธ๋ž˜์˜ ๊ณ ์œ  ๋ฒˆํ˜ธ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”.

    ์ œํ•œ์‚ฌํ•ญ

    • genres[i]๋Š” ๊ณ ์œ ๋ฒˆํ˜ธ๊ฐ€ i์ธ ๋…ธ๋ž˜์˜ ์žฅ๋ฅด์ž…๋‹ˆ๋‹ค.
    • plays[i]๋Š” ๊ณ ์œ ๋ฒˆํ˜ธ๊ฐ€ i์ธ ๋…ธ๋ž˜๊ฐ€ ์žฌ์ƒ๋œ ํšŸ์ˆ˜์ž…๋‹ˆ๋‹ค.
    • genres์™€ plays์˜ ๊ธธ์ด๋Š” ๊ฐ™์œผ๋ฉฐ, ์ด๋Š” 1 ์ด์ƒ 10,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
    • ์žฅ๋ฅด ์ข…๋ฅ˜๋Š” 100๊ฐœ ๋ฏธ๋งŒ์ž…๋‹ˆ๋‹ค.
    • ์žฅ๋ฅด์— ์†ํ•œ ๊ณก์ด ํ•˜๋‚˜๋ผ๋ฉด, ํ•˜๋‚˜์˜ ๊ณก๋งŒ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.
    • ๋ชจ๋“  ์žฅ๋ฅด๋Š” ์žฌ์ƒ๋œ ํšŸ์ˆ˜๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

    ์ž…์ถœ๋ ฅ ์˜ˆ

    genresplaysreturn

    [classic, pop, classic, classic, pop] [500, 600, 150, 800, 2500] [4, 1, 3, 0]

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

    classic ์žฅ๋ฅด๋Š” 1,450ํšŒ ์žฌ์ƒ๋˜์—ˆ์œผ๋ฉฐ, classic ๋…ธ๋ž˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    • ๊ณ ์œ  ๋ฒˆํ˜ธ 3: 800ํšŒ ์žฌ์ƒ
    • ๊ณ ์œ  ๋ฒˆํ˜ธ 0: 500ํšŒ ์žฌ์ƒ
    • ๊ณ ์œ  ๋ฒˆํ˜ธ 2: 150ํšŒ ์žฌ์ƒ

    pop ์žฅ๋ฅด๋Š” 3,100ํšŒ ์žฌ์ƒ๋˜์—ˆ์œผ๋ฉฐ, pop ๋…ธ๋ž˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    • ๊ณ ์œ  ๋ฒˆํ˜ธ 4: 2,500ํšŒ ์žฌ์ƒ
    • ๊ณ ์œ  ๋ฒˆํ˜ธ 1: 600ํšŒ ์žฌ์ƒ

    ๋”ฐ๋ผ์„œ pop ์žฅ๋ฅด์˜ [4, 1]๋ฒˆ ๋…ธ๋ž˜๋ฅผ ๋จผ์ €, classic ์žฅ๋ฅด์˜ [3, 0]๋ฒˆ ๋…ธ๋ž˜๋ฅผ ๊ทธ๋‹ค์Œ์— ์ˆ˜๋กํ•ฉ๋‹ˆ๋‹ค.

    #include <string>
    #include <vector>
    #include <algorithm>
    #include <map>
    #include <iostream>
    
    using namespace std;
    bool comp(pair<string, int> a, pair<string, int> b) {
    	return a.second > b.second;
    }
    bool comp2(pair<int, int> a, pair<int, int> b) {
    	if (a.first == b.first) {
    		return a.second < b.second;
    	}
    	return a.first > b.first;
    }
    vector<int> solution(vector<string> genres, vector<int> plays) {
    	vector<int> answer;
    	map<string, int> total_play;
    	map<string, int>::iterator it;
    	map<string, vector<pair<int, int>>> list;
    	for (int i = 0; i < plays.size(); i++) {
    		total_play[genres[i]] += plays[i];
    		list[genres[i]].push_back(pair<int, int>(plays[i], i));
    	}
    	int i = 0;
    	vector<pair<string, int>> total_playName;
    	for (it = total_play.begin(); it != total_play.end(); it++, i++) {
    		total_playName.push_back(pair<string, int>(it->first, it->second));
    	}
    	sort(total_playName.begin(), total_playName.end(), comp);
    
    	for (int i = 0; i < total_playName.size(); i++) {
    		sort(list[total_playName[i].first].begin(), list[total_playName[i].first].end(), comp2);
    		for (int j = 0; j < 2 && j < list[total_playName[i].first].size(); j++) {
    			answer.push_back(list[total_playName[i].first][j].second);
    		}
    	}
    
    	return answer;
    }
    int main()
    {
    	vector<string> genres;
    	vector<int> plays;
    	vector<int> ans;
    
    	genres.push_back("classic");
    	genres.push_back("pop");
    	genres.push_back("classic");
    	genres.push_back("classic");
    	genres.push_back("pop");
    
    	plays.push_back(500);
    	plays.push_back(600);
    	plays.push_back(150);
    	plays.push_back(800);
    	plays.push_back(2500);
    
    	ans = solution(genres, plays);
    
    	for(int i=0;i<ans.size(); i++)
    		cout << ans[i] << endl;
    }
    728x90

    ๋Œ“๊ธ€

Designed by Tistory.