ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C++] ํ”„๋ฆฐํ„ฐ
    ์ฝ”๋”ฉํ…Œ์ŠคํŠธ/์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต 2019. 9. 21. 23:55
    728x90

    ๋ฌธ์ œ ์„ค๋ช…

    ์ผ๋ฐ˜์ ์ธ ํ”„๋ฆฐํ„ฐ๋Š” ์ธ์‡„ ์š”์ฒญ์ด ๋“ค์–ด์˜จ ์ˆœ์„œ๋Œ€๋กœ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ค‘์š”ํ•œ ๋ฌธ์„œ๊ฐ€ ๋‚˜์ค‘์— ์ธ์‡„๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ์ค‘์š”๋„๊ฐ€ ๋†’์€ ๋ฌธ์„œ๋ฅผ ๋จผ์ € ์ธ์‡„ํ•˜๋Š” ํ”„๋ฆฐํ„ฐ๋ฅผ ๊ฐœ๋ฐœํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ์ƒˆ๋กญ๊ฒŒ ๊ฐœ๋ฐœํ•œ ํ”„๋ฆฐํ„ฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ธ์‡„ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

    1. ์ธ์‡„ ๋Œ€๊ธฐ๋ชฉ๋ก์˜ ๊ฐ€์žฅ ์•ž์— ์žˆ๋Š” ๋ฌธ์„œ(J)๋ฅผ ๋Œ€๊ธฐ๋ชฉ๋ก์—์„œ ๊บผ๋ƒ…๋‹ˆ๋‹ค. 2. ๋‚˜๋จธ์ง€ ์ธ์‡„ ๋Œ€๊ธฐ๋ชฉ๋ก์—์„œ J๋ณด๋‹ค ์ค‘์š”๋„๊ฐ€ ๋†’์€ ๋ฌธ์„œ๊ฐ€ ํ•œ ๊ฐœ๋ผ๋„ ์กด์žฌํ•˜๋ฉด J๋ฅผ ๋Œ€๊ธฐ๋ชฉ๋ก์˜ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋„ฃ์Šต๋‹ˆ๋‹ค. 3. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด J๋ฅผ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค.

    ์˜ˆ๋ฅผ ๋“ค์–ด, 4๊ฐœ์˜ ๋ฌธ์„œ(A, B, C, D)๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ์ธ์‡„ ๋Œ€๊ธฐ๋ชฉ๋ก์— ์žˆ๊ณ  ์ค‘์š”๋„๊ฐ€ 2 1 3 2 ๋ผ๋ฉด C D A B ์ˆœ์œผ๋กœ ์ธ์‡„ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

    ๋‚ด๊ฐ€ ์ธ์‡„๋ฅผ ์š”์ฒญํ•œ ๋ฌธ์„œ๊ฐ€ ๋ช‡ ๋ฒˆ์งธ๋กœ ์ธ์‡„๋˜๋Š”์ง€ ์•Œ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์—์„œ C๋Š” 1๋ฒˆ์งธ๋กœ, A๋Š” 3๋ฒˆ์งธ๋กœ ์ธ์‡„๋ฉ๋‹ˆ๋‹ค.

    ํ˜„์žฌ ๋Œ€๊ธฐ๋ชฉ๋ก์— ์žˆ๋Š” ๋ฌธ์„œ์˜ ์ค‘์š”๋„๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ๋‹ด๊ธด ๋ฐฐ์—ด priorities์™€ ๋‚ด๊ฐ€ ์ธ์‡„๋ฅผ ์š”์ฒญํ•œ ๋ฌธ์„œ๊ฐ€ ํ˜„์žฌ ๋Œ€๊ธฐ๋ชฉ๋ก์˜ ์–ด๋–ค ์œ„์น˜์— ์žˆ๋Š”์ง€๋ฅผ ์•Œ๋ ค์ฃผ๋Š” location์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋‚ด๊ฐ€ ์ธ์‡„๋ฅผ ์š”์ฒญํ•œ ๋ฌธ์„œ๊ฐ€ ๋ช‡ ๋ฒˆ์งธ๋กœ ์ธ์‡„๋˜๋Š”์ง€ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

    ์ œํ•œ์‚ฌํ•ญ

    • ํ˜„์žฌ ๋Œ€๊ธฐ๋ชฉ๋ก์—๋Š” 1๊ฐœ ์ด์ƒ 100๊ฐœ ์ดํ•˜์˜ ๋ฌธ์„œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์ธ์‡„ ์ž‘์—…์˜ ์ค‘์š”๋„๋Š” 1~9๋กœ ํ‘œํ˜„ํ•˜๋ฉฐ ์ˆซ์ž๊ฐ€ ํด์ˆ˜๋ก ์ค‘์š”ํ•˜๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.
    • location์€ 0 ์ด์ƒ (ํ˜„์žฌ ๋Œ€๊ธฐ๋ชฉ๋ก์— ์žˆ๋Š” ์ž‘์—… ์ˆ˜ - 1) ์ดํ•˜์˜ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ ๋Œ€๊ธฐ๋ชฉ๋ก์˜ ๊ฐ€์žฅ ์•ž์— ์žˆ์œผ๋ฉด 0, ๋‘ ๋ฒˆ์งธ์— ์žˆ์œผ๋ฉด 1๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.

    ์ž…์ถœ๋ ฅ ์˜ˆ

    priorities location return
    [2, 1, 3, 2] 2 1
    [1, 1, 9, 1, 1, 1] 0 5

     

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

    ์˜ˆ์ œ #1

    ๋ฌธ์ œ์— ๋‚˜์˜จ ์˜ˆ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    ์˜ˆ์ œ #2

    6๊ฐœ์˜ ๋ฌธ์„œ(A, B, C, D, E, F)๊ฐ€ ์ธ์‡„ ๋Œ€๊ธฐ๋ชฉ๋ก์— ์žˆ๊ณ  ์ค‘์š”๋„๊ฐ€ 1 1 9 1 1 1 ์ด๋ฏ€๋กœ C D E F A B ์ˆœ์œผ๋กœ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค.

    #include <string>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <utility>
    
    using namespace std;
    
    int solution(vector<int> priorities, int location) {
    	int answer = 1;
    	vector<pair<int, int>> stack;
    	vector<pair<int, int>>::iterator iter;
    	vector<int> priority;
    
    	for (int i = 0; i < priorities.size(); i++) {
    		stack.push_back(make_pair(priorities[i], i));
    		priority.push_back(priorities[i]);
    	}
    
    	sort(priority.begin(), priority.end());
    
    	while (!stack.empty())
    	{
    		pair<int, int> temp;
    		temp = stack.front();
    		stack.erase(stack.begin());
    
    		if (temp.first == priority.back())
    		{
    			if (location == temp.second)
    				return answer;
    			priority.pop_back();
    			answer++;
    		}
    		else stack.push_back(temp);
    	}
    	return answer;
    }
    int main()
    {
    	vector<int> priorities = { 3, 3, 4, 2 };
    	int ans;
    	
    	ans = solution(priorities, 3);
    	cout << ans << endl;
    
    	/*for(int i=0;i<ans.size(); i++)
    		cout << ans[i] << endl;*/
    }
    728x90

    ๋Œ“๊ธ€

Designed by Tistory.