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

Partially Committed

[์›”๊ฐ„ ์ฝ”๋“œ ์ฑŒ๋ฆฐ์ง€ ์‹œ์ฆŒ1] ๋‘ ๊ฐœ ๋ฝ‘์•„์„œ ๋”ํ•˜๊ธฐ ๋ณธ๋ฌธ

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

[์›”๊ฐ„ ์ฝ”๋“œ ์ฑŒ๋ฆฐ์ง€ ์‹œ์ฆŒ1] ๋‘ ๊ฐœ ๋ฝ‘์•„์„œ ๋”ํ•˜๊ธฐ

WonderJay 2022. 7. 4. 18:50
728x90
๋ฐ˜์‘ํ˜•
SMALL

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋‘ ๊ฐœ ๋ฝ‘์•„์„œ ๋”ํ•˜๊ธฐ

์ •์ˆ˜ ๋ฐฐ์—ด numbers๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. numbers์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ์ธ๋ฑ์Šค์— ์žˆ๋Š” ๋‘ ๊ฐœ์˜ ์ˆ˜๋ฅผ ๋ฝ‘์•„ ๋”ํ•ด์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์ˆ˜๋ฅผ ๋ฐฐ์—ด์— ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. ์ œํ•œ

programmers.co.kr

numbers ๋ฐฐ์—ด์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ์ธ๋ฑ์Šค์˜ 2 ๊ฐœ ์ˆซ์ž๋ฅผ ๋”ํ•ด์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์ˆ˜๋ฅผ answer ๋ฐฐ์—ด์— ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋‹ด์•„ return ํ•˜๋„๋ก ํ•ด์•ผํ•œ๋‹ค.

 

ํ’€์ด1. ์ด ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ๋ฐ”๋กœ ๋– ์˜ค๋ฅธ ๋ฐฉ๋ฒ•์€, numbers ๋ฐฐ์—ด์—์„œ 2 ๊ฐœ๋ฅผ ์„ ํƒ(combination)ํ•˜๊ณ  ๋”ํ•œ ๊ฐ’์„ ์ค‘๋ณต ์—†์ด answer ๋ฐฐ์—ด์— ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์ด์šฉํ•˜์˜€๋‹ค.

 

[C++]

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

using namespace std;

int sum = 0;
vector<int> arr(2, 0);

bool check(int x, vector<int>& answer)
{
    for (auto& ele : answer)
    {
        if (ele == x)
        {
            return false;
        }
    }
    return true;
}

void dfs(int depth, int start, vector<int>& numbers, vector<int>& answer)
{
    if (depth == 2)
    {
        // sum ๊ณ„์‚ฐ
        sum = arr[0] + arr[1];

        if (check(sum, answer) == true) //answer ๋ฐฐ์—ด์— sum ์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด
        {
            answer.push_back(sum); // answer ๋ฐฐ์—ด์— ๋„ฃ๊ณ 
        }
        sum = 0; // sum ์„ ๋‹ค์‹œ 0์œผ๋กœ ํ•˜๊ณ  return
        return;
    }
    else
    {
        for (int i = start; i < numbers.size(); i++)
        {
            arr[depth] = numbers[i];
            dfs(depth + 1, i + 1, numbers, answer);
        }
    }
}

vector<int> solution(vector<int> numbers) {
    vector<int> answer;

    dfs(0, 0, numbers, answer);

    sort(answer.begin(), answer.end());

    return answer;
}

 

ํ’€์ด2. ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ๊ตฌ๊ฒฝํ•˜๋‹ค๊ฐ€, ๋‚ด๊ฐ€ ์กฐ๊ธˆ ๋ณต์žกํ•˜๊ฒŒ ์ƒ๊ฐํ•œ ์ธก๋ฉด์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ฌ์•˜๋‹ค. ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„, ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” ์ปจํ…Œ์ด๋„ˆ์ธ set ์„ ์ด์šฉํ•˜๋ฉด ๋ณด๋‹ค ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. numbers ๋ฐฐ์—ด์—์„œ 2๊ฐœ์”ฉ ๋ฝ‘์•„์„œ ๋”ํ•œ ๊ฒƒ์„ set ์ปจํ…Œ์ด๋„ˆ์— ๋„ฃ์–ด์ฃผ๋ฉด ์ž๋™์œผ๋กœ ์ค‘๋ณต์ด ์ œ๊ฑฐ๋˜๊ณ , answer ์— ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค. set, map ์€ ์ž๋™์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜๋ฏ€๋กœ ๋ณ„๋„์˜ ์ •๋ ฌ์€ ํ•„์š”์—†๋‹ค.

 

[C++]

 

#include <string>
#include <vector>
#include <set>

using namespace std;


vector<int> solution(vector<int> numbers) {
    vector<int> answer;
    set<int> s;

    for (int i = 0; i < numbers.size(); i++)
    {
        for (int j = i+1; j < numbers.size(); j++)
        {
            s.insert(numbers[i] + numbers[j]);
        }
    }
    answer.assign(s.begin(), s.end());

    return answer;
}

 

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