Notice
Recent Posts
Recent Comments
Today
Total
01-26 01:17
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

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

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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฒด์œก๋ณต

WonderJay 2022. 7. 3. 00:11
728x90
๋ฐ˜์‘ํ˜•
SMALL

https://programmers.co.kr/learn/courses/30/lessons/42862

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ฒด์œก๋ณต

์ ์‹ฌ์‹œ๊ฐ„์— ๋„๋‘‘์ด ๋“ค์–ด, ์ผ๋ถ€ ํ•™์ƒ์ด ์ฒด์œก๋ณต์„ ๋„๋‚œ๋‹นํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹คํ–‰ํžˆ ์—ฌ๋ฒŒ ์ฒด์œก๋ณต์ด ์žˆ๋Š” ํ•™์ƒ์ด ์ด๋“ค์—๊ฒŒ ์ฒด์œก๋ณต์„ ๋นŒ๋ ค์ฃผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ํ•™์ƒ๋“ค์˜ ๋ฒˆํ˜ธ๋Š” ์ฒด๊ฒฉ ์ˆœ์œผ๋กœ ๋งค๊ฒจ์ ธ ์žˆ์–ด, ๋ฐ”๋กœ ์•ž๋ฒˆ

programmers.co.kr


ํ’€์ด1.

์ฒ˜์Œ์— ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ๋ฐ”๋กœ ๋– ์˜ค๋ฅธ ๋ฐฉ๋ฒ•์ด๋‹ค. ๊ทธ๋ž˜์„œ ๊ทธ๋Ÿฐ์ง€ ๋‹ค์†Œ ๋…ผ๋ฆฌ๊ฐ€ ๊น”๋”ํ•˜์ง€๋Š” ๋ชปํ•œ ๊ฒƒ ๊ฐ™๋‹ค...

 

1. hash ๋งต์— ํ˜„์žฌ ํ•™์ƒ๋“ค์˜ ์ฒด์œก๋ณต ๊ฐœ์ˆ˜ ํ˜„ํ™ฉ์„ ์ €์žฅํ•œ๋‹ค.

 

2. ์ฒด์œก๋ณต์ด ์—†๋Š” ํ•™์ƒ์€ ์•ž๋’ท๋ฒˆํ˜ธ์˜ ํ•™์ƒ์ด ์ฒด์œก๋ณต ์—ฌ๋ถ„์ด ์žˆ๋‹ค๋ฉด ์ฒด์œก๋ณต์„ ํ•˜๋‚˜ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด i ๋ฒˆ์งธ ํ•™์ƒ์ด ์ฒด์œก๋ณต์ด ์—†๋‹ค๋ฉด i-1 ๋ฒˆ์งธ, i+1 ๋ฒˆ์งธ ํ•™์ƒ์ด ์—ฌ๋ถ„์ด ์žˆ๋‹ค๋ฉด i ๋ฒˆ์งธ ํ•™์ƒ์—๊ฒŒ ํ•˜๋‚˜๋ฅผ ์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

3. i-1 ๋ฒˆ์งธ ํ•™์ƒ์ด i ๋ฒˆ์งธ ํ•™์ƒ์—๊ฒŒ ์ฒด์œก๋ณต์„ ์ค€ ๊ฒฝ์šฐ์™€ i+1 ๋ฒˆ์งธ ํ•™์ƒ์ด i ๋ฒˆ์งธ ํ•™์ƒ์—๊ฒŒ ์ฒด์œก๋ณต์„ ์ค€ ๋‘ ๊ฐ€์ง€ ์ƒํ™ฉ์— ๋Œ€ํ•ด์„œ ์ฒด์œก๋ณต์„ 1๊ฐœ ์ด์ƒ ๊ฐ€์ง„ ํ•™์ƒ์ด ๋งŽ์€ ์ƒํ™ฉ์„ ํƒํ•˜๋„๋ก ํ•œ๋‹ค.

( ํ˜„์žฌ ์ƒํ™ฉ์—์„œ ์ตœ๋Œ€ํ•œ ์ข‹์€ ๊ฒฐ๊ณผ๋กœ ๋ณด์ด๋Š” ์„ ํƒ์„ ๋ฐ˜๋ณตํ•œ๋‹ค. )

 

4. ์ด๋•Œ i-1 ๋ฒˆ์งธ ํ•™์ƒ๊ณผ i+1 ๋ฒˆ์งธ ํ•™์ƒ์ด ๋™์‹œ์— ์—ฌ๋ถ„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด, ์•ž ์ˆœ์„œ์˜ ํ•™์ƒ์—๊ฒŒ ์—ฌ๋ถ„์„ ๋ฐ›๋„๋ก ์ฒ˜๋ฆฌํ•œ๋‹ค.

 

[C++]

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

using namespace std;

int check_max(map<int, int> h_map)
{
    int sum = 0;
    for (auto& ele : h_map)
    {
        if (ele.second > 0)
            sum++;
    }
    return sum;
}

int solution(int n, vector<int> lost, vector<int> reserve) {
    int answer = 0;

    map<int, int> h_map;

    for (int i = 1; i <= n; i++)
    {
        h_map.insert(make_pair(i, 1));
    }

    for (auto& ele : reserve)
    {
        h_map[ele]++;
    }

    for (auto& ele : lost)
    {
        h_map[ele]--;
    }
    
    if (h_map[1] == 0 && h_map[2] == 2)
    {
        h_map[1]++;
        h_map[2]--;
    }
 
    bool flag = false;
    for (int i = 2; i <= n; i++)
    {
        int temp1 = 0; int temp2 = 0;
        if (h_map[i] == 0) // lost
        {
            if (flag==false&&h_map[i - 1] == 2)
            {
                h_map[i - 1]--;
                h_map[i]++;

                temp1 = check_max(h_map);

                h_map[i]--;
                h_map[i - 1]++;

                flag = true;
            }
            if (flag == false&&h_map[i + 1] == 2)
            {
                h_map[i + 1]--;
                h_map[i]++;

                temp2 = check_max(h_map);
                
                h_map[i]--;
                h_map[i + 1]++;

                flag = true;
            }
            if (flag == false&&h_map[i - 1] == 2 && h_map[i+1] == 2)
            {
                h_map[i - 1]--;
                h_map[i]++;

                temp1 = check_max(h_map);

                h_map[i]--;
                h_map[i - 1]++;

                flag = true;
            }
            if (flag == true)
            {
                if (temp1 > temp2)
                {
                    h_map[i - 1]--;
                    h_map[i]++;

                    flag = false;
                    continue;
                }
                else
                {
                    h_map[i + 1]--;
                    h_map[i]++;

                    flag = false;
                    continue;
                }
            }


        }   
    }

    answer = check_max(h_map);

    return answer;
}

 

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