Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

13-oesnuj #44

Merged
merged 5 commits into from
Sep 4, 2024
Merged

13-oesnuj #44

merged 5 commits into from
Sep 4, 2024

Conversation

oesnuj
Copy link
Member

@oesnuj oesnuj commented Jul 27, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋ฐฑ์ค€ | ๋ถ€๋…€ํšŒ์žฅ์ด ๋ ํ…Œ์•ผ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

solved.ac class2์— ๋‚จ์€ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ์ธ๋ฐ DP๋กœ ๋ถ„๋ฅ˜๋˜์–ด์žˆ๊ธธ๋ž˜ ๋งˆ๋ฌด๋ฆฌ ํ• ๊ฒธ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค.

๋จผ์ € ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ์ฝ์œผ๋‹ˆ ์นœ์ ˆํ•˜๊ฒŒ ์žฌ๊ท€๋ฅผ ์“ฐ๋ผ๋Š” ๋งํ•˜๋Š” ๊ฒƒ ๊ฐ™์•˜์Šต๋‹ˆ๋‹ค.
k์ธต์— ๋”ฐ๋ฅธ 1๋ถ€ํ„ฐ nํ˜ธ๊นŒ์ง€์˜ ์‚ฌ๋žŒ์ˆ˜์˜ ํ•ฉ์ด๋ผ...
image

1๏ธโƒฃ ํ’€์ด 1 ์žฌ๊ท€๋งŒ ์ด์šฉ

๊ทผ๋ฐ c++์ด๋ผ ๊ทธ๋Ÿฐ์ง€๋Š” ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ ์žฌ๊ท€๋งŒ ์ผ๋Š”๋ฐ๋„ ํ’€๋ ธ์Šต๋‹ˆ๋‹ค.
๋ชจ๋“  ์žฌ๊ท€ ๋ฌธ์ œ๊ฐ€ ๊ทธ๋ ‡๋“ฏ ๋ฌธ์ œ์˜ ์ •์˜ + ํƒˆ์ถœ์กฐ๊ฑด์„ ์ œ๋Œ€๋กœ ๋งŒ๋“ค๋ฉด ๋ฉ๋‹ˆ๋‹ค.
๋ฌธ์ œ์—์„œ k์ธต nํ˜ธ์˜ ์‚ฌ๋žŒ์ˆ˜๋Š” k-1์ธต์˜ 1ํ˜ธ๋ถ€ํ„ฐ nํ˜ธ๊นŒ์ง€์˜ ์‚ฌ๋žŒ ์ˆ˜์˜ ํ•ฉ์ด๋ผ๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค.

Note

๋ฌธ์ œ์˜ ์ •์˜ : k์ธต nํ˜ธ์˜ ์‚ฌ๋žŒ์ˆ˜ = k-1์ธต์˜ 1ํ˜ธ๋ถ€ํ„ฐ nํ˜ธ๊นŒ์ง€์˜ ์‚ฌ๋žŒ ์ˆ˜์˜ ํ•ฉ
ํƒˆ์ถœ ์กฐ๊ฑด : 0์ธต์ผ ๊ฒฝ์šฐ ์‚ฌ๋žŒ์ˆ˜(n) ๋ฆฌํ„ด

์œ„ ๋‘๊ฐ€์ง€๋ฅผ ํ™œ์šฉํ•ด ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค!!!

/*ํ’€์ด1 ์žฌ๊ท€๋งŒ ์ด์šฉ*/
#include <iostream>
using namespace std;

int calc(int k, int n) {
    if (k == 0) return n; //0์ธต์ธ ๊ฒฝ์šฐ n๋ช… ๋ฆฌํ„ด
    //0์ธต์ด ์•„๋‹Œ ๊ฒฝ์šฐ๋ผ๋ฉด ํ•œ์นธ ์•„๋ž˜์ธต์˜ 1ํ˜ธ๋ถ€ํ„ฐ nํ˜ธ๊นŒ์ง€์˜ ์‚ฌ๋žŒ์ˆ˜ ํ•ฉ์„ ๋ฆฌํ„ด
    int sum = 0;
    for (int i = n; i > 0; i--) {
        sum += calc(k - 1, i);
    }
    return sum;
}

int main() 
{
    int T, k, n;
    cin >> T;
    while (T--) {
        cin >> k >> n;
        cout << calc(k, n) << "\n";
    }
    return 0;
}

2๏ธโƒฃ ํ’€์ด2 ์žฌ๊ท€ + DP

DP๋Š” ๋™์ผํ•œ ๊ณ„์‚ฐ์„ ๋ฐ˜๋ณตํ•ด์•ผ ํ•  ๋•Œ, ์ด์ „์— ๊ณ„์‚ฐํ•œ ๊ฐ’์„ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•จ์œผ๋กœ์จ ๋™์ผํ•œ ๊ณ„์‚ฐ์˜ ๋ฐ˜๋ณต ์ˆ˜ํ–‰์„ ์ œ๊ฑฐํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์†๋„๋ฅผ ๋น ๋ฅด๊ฒŒ ํ•˜๋Š” ๊ธฐ์ˆ ์ด๋‹ค.
์ด ๋ฌธ์ œ์—์„œ๋Š” k, n์— ๋”ฐ๋ฅธ ํ•จ์ˆ˜ํ˜ธ์ถœ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•ด๋‘์–ด ์žฌ๊ท€ ํ˜ธ์ถœ์—์„œ ์ด๋ฏธ ํ˜ธ์ถœ๋œ ์ ์ด ์žˆ๋Š” k, n์˜ ๊ฐ’์— ๋Œ€ํ•ด์„œ๋Š” ์ €์žฅ๋œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค!

[์ถ”๊ฐ€๋œ ๋ถ€๋ถ„]

  • ํƒˆ์ถœ ์กฐ๊ฑด์— ํ˜ธ์ถœ๋œ์  ์žˆ๋Š” ๊ฒฝ์šฐ dpํ…Œ์ด๋ธ”์˜ ๊ฐ’์„ ๋ฆฌํ„ด
  • ์ฒซ ํ˜ธ์ถœ์ธ ๊ฒฝ์šฐ, ๊ฒฐ๊ณผ๋ฅผ dpํ…Œ์ด๋ธ”์— ์ €์žฅํ•˜๊ณ  ๋ฐ˜ํ™˜
  • ๋งค ์ผ€์ด์Šค๋งˆ๋‹ค dpํ…Œ์ด๋ธ” ์ดˆ๊ธฐํ™”
/*ํ’€์ด2, ์žฌ๊ท€ + DP*/
#include <iostream>
#include <vector>
using namespace std;

vector<vector<int>> dp;

int calc(int k, int n) {
    if (k == 0) return n;
    if (dp[k][n] != -1) return dp[k][n]; // ์ด๋ฏธ ๊ณ„์‚ฐ๋œ ๊ฐ’์ด๋ฉด ๋ฐ˜ํ™˜

    int sum = 0;
    for (int i = n; i > 0; i--) {
        sum += calc(k - 1, i);
    }
    return dp[k][n] = sum; // ๊ณ„์‚ฐ๋œ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  ๋ฐ˜ํ™˜
}

int main()
{
    int T, k, n;
    cin >> T;
    while (T--) {
        cin >> k >> n;
        dp = vector<vector<int>>(k + 1, vector<int>(n + 1, -1)); //์ผ€์ด์Šค๋ณ„ DP ํ…Œ์ด๋ธ” ์ดˆ๊ธฐํ™”
        cout << calc(k, n) << "\n";
    }
    return 0;
}

3๏ธโƒฃ ์†๋„ ์ฐจ์ด

์œ„๊ฐ€ DP ํ’€์ด์ธ๋ฐ ์‹œ๊ฐ„์„ ๋ณด๋‹ˆ ์ฐจ์ด๊ฐ€ ํ™•์‹คํžˆ๋‚˜๋„ค์š”
image

๋‹จ์ˆœ ์žฌ๊ท€: ์‹œ๊ฐ„๋ณต์žก๋„ O(n^k)๋กœ k์™€ n์ด ์ปค์งˆ์ˆ˜๋ก ๊ณ„์‚ฐ๋Ÿ‰์ด ๋งŽ์ด ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

์žฌ๊ท€ + DP: ์‹œ๊ฐ„ ๋ณต์žก๋„ O(k * n^2)๋กœ ์ค‘๋ณต๊ณ„์‚ฐ์„ ํ”ผํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ํ›จ์”ฌ ์ค„์–ด๋“ญ๋‹ˆ๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

DPํ…Œ์ด๋ธ”์„ ํ™œ์šฉํ•œ ๋ฉ”๋ชจ์ œ์ด์…˜ ๋ฐฉ์‹์œผ๋กœ ๋” ํšจ์œจ์ ์œผ๋กœ ์žฌ๊ท€ํ˜ธ์ถœ์„ ํ•œ๋‹ค!

๋˜ ๋‹ค๋ฅธ ํ’€์ด๋กœ 0์ธต 1ํ˜ธ ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์ฆ๊ฐ€์‹œ์ผฐ์„๋•Œ์˜ ๊ทœ์น™์œผ๋กœ ์ ํ™”์‹์„ ์„ธ์›Œ ํ‘ธ๋Š” ๋ฐฉ์‹๋„ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ ๋‹ค๋“ค ํ’€์–ด๋ณด์…จ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ’€์—ˆ๋Š”์ง€ ๊ถ๊ธˆํ•˜๋„ค์š”.

Copy link
Collaborator

@pu2rile pu2rile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ฒ˜์Œ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์„ ๋•Œ ๋‹จ์ˆœํ•˜๊ฒŒ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ํ’€์—ˆ๋Š”๋ฐ ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•˜๊ธด ํ•˜์ง€๋งŒ ์ค€์„œ๋‹˜์ฒ˜๋Ÿผ dp๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ํ›จ์”ฌ ํšจ์œจ์ ์ด๊ฒ ๋”๋ผ๊ตฌ์š”

๋ฐ˜๋ณต๋ฌธ ์ฝ”๋“œ

T = int(input())  # ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜ ์ž…๋ ฅ

for _ in range(T):
    k = int(input())  # ์ธต ์ˆ˜ ์ž…๋ ฅ
    n = int(input())  # ํ˜ธ ์ˆ˜ ์ž…๋ ฅ

    # 0์ธต์˜ ๊ฐ ํ˜ธ์ˆ˜์— ์žˆ๋Š” ์‚ฌ๋žŒ ์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”
    people = [i for i in range(1, n+1)]

    # k์ธต๊นŒ์ง€์˜ ๊ฐ ํ˜ธ์ˆ˜์— ์žˆ๋Š” ์‚ฌ๋žŒ ์ˆ˜๋ฅผ ๊ณ„์‚ฐ
    for _ in range(k):
        for i in range(1, n):
            people[i] += people[i-1]
    print(people[-1])  # k์ธต nํ˜ธ์— ์žˆ๋Š” ์‚ฌ๋žŒ ์ˆ˜ ์ถœ๋ ฅ

dp ์ฝ”๋“œ

def calc(k, n, dp):
    if k == 0:
        return n
    if dp[k][n] != -1:
        return dp[k][n]  # ์ด๋ฏธ ๊ณ„์‚ฐ๋œ ๊ฐ’์ด๋ฉด ๋ฐ˜ํ™˜

    sum_ = 0
    for i in range(1, n + 1):
        sum_ += calc(k - 1, i, dp)
    dp[k][n] = sum_  # ๊ณ„์‚ฐ๋œ ๊ฐ’์„ ์ €์žฅ
    return sum_

T = int(input())  # ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜ ์ž…๋ ฅ

for _ in range(T):
    k = int(input())  # ์ธต ์ˆ˜ ์ž…๋ ฅ
    n = int(input())  # ํ˜ธ ์ˆ˜ ์ž…๋ ฅ
    
    # DP ํ…Œ์ด๋ธ” ์ดˆ๊ธฐํ™”
    dp = [[-1] * (n + 1) for _ in range(k + 1)]
    
    # ๊ฒฐ๊ณผ ๊ณ„์‚ฐ ๋ฐ ์ถœ๋ ฅ
    result = calc(k, n, dp)
    print(result)

์ €๋„ ๊ฒฐ๊ตญ ์ค€์„œ๋‹˜์ด๋ž‘ ๊ฐ™์€ ๋กœ์ง์œผ๋กœ ํ’€์–ด์„œ ๋‹ค๋ฅธ ๋ฐฉ์‹์„ ์ƒ๊ฐํ•ด๋‚ด์ง„ ๋ชปํ–ˆ์ง€๋งŒ... dp ์—ฐ์Šต์œผ๋กœ ์ข‹์€ ๋ฌธ์ œ์˜€๋˜ ๊ฒƒ ๊ฐ™์•„์š”!! pr ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค

Copy link

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์‚ฌ์‹ค ์š”๊ฑด ๋ฐ”ํ…€์—… ๋ฐฉ์‹์ด ๋”ฑ ์ง๊ด€์ ์ด๊ธด ํ•ฉ๋‹ˆ๋‹ค :)

#include <stdio.h>
#include <memory.h>

int t, k, n;
int apartment[15][15];

int main()
{
    scanf(" %d", &t);
    while(t--)
    {
        scanf(" %d %d", &k, &n);
        memset(apartment, 0, sizeof(apartment));
        
        for(int number = 1; number <= n; ++number)
        {
            apartment[0][number] = number;
        }
        
        for(int floor = 1; floor <= k; ++floor)
        {
            for(int number = 1; number <= n; ++number)
            {
                apartment[floor][number] 
                    = apartment[floor - 1][number] + apartment[floor][number - 1];
            }
        }
        
        printf("%d\n", apartment[k][n]);
    }

    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants