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

7-rivkms #25

Merged
merged 2 commits into from
Mar 11, 2024
Merged

7-rivkms #25

merged 2 commits into from
Mar 11, 2024

Conversation

rivkms
Copy link
Collaborator

@rivkms rivkms commented Mar 2, 2024

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

์—ฐ์†ํ•ฉ

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

1h

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

์›๋ž˜ ์ด๋ฒˆ์ฐจ์‹œ์— ๋‹ค๋ฅธ ๋ฌธ์ œ๋ฅผ ๊ฐ€์ ธ์˜ค๋ ค๊ณ  ํ•˜๋‹ค๊ฐ€ ํ’€์ด์— ์‹คํŒจํ•˜์—ฌ ์ด ๋ฌธ์ œ๋ฅผ ๊ฐ€์ ธ์˜ค๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค...
์‰ฌ์šด ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™์œผ๋‚˜, ์ตœ์„ ์„ ๋‹คํ•ด ์„ค๋ช…ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

โœ”๏ธ ๋ฌธ์ œ ์œ ํ˜•

: DP

์ €๋Š” ์ด ๋ฌธ์ œ๋ฅผ DP๋กœ ํ’€์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

์ ‘๊ทผ ๋ฐฉ๋ฒ•

์ €๋Š” ์ด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•˜์—ฌ Bottom-up ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•ด๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค!
์šฐ์„  DP-table์„ ์–ด๋–ป๊ฒŒ ๊ตฌํ• ์ง€ ์ƒ๊ฐํ•ด๋ด์•ผ๊ฒ ์ง€์š”.
์ €๋Š” DP-table์— n๋ฒˆ์งธ๊นŒ์ง€๋ฅผ ๋”ํ•  ์ˆ˜ ์žˆ์„ ๋•Œ์˜ ์ตœ๋Œ“๊ฐ’์„ ์ €์žฅํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ž…๋ ฅ์ด ๋“ค์–ด์™”๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

10
10 -4 3 1 5 6 -35 12 21 -1

๊ทธ๋ ‡๋‹ค๋ฉด DP-table์—๋Š” ์ด๋ ‡๊ฒŒ ์ €์žฅ๋˜๊ฒ ์ง€์š”
10 6 9 10 15 21 -14 12 33 32

์ด ๊ฒฝ์šฐ์—์„œ -14 ๋‹ค์Œ์— 12๊ฐ€ ์˜ค๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ

1๏ธโƒฃ ์•ž์—์„œ ์ตœ๋Œ€์ธ ๊ฐ’์—์„œ n๋ฒˆ์งธ ์ˆ˜๋ฅผ ๋”ํ•œ ๊ฒƒ
2๏ธโƒฃ n๋ฒˆ์งธ ์ˆ˜

๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋” ํฐ ๊ฐ’์ด DP-table์— ๋“ค์–ด๊ฐ€๋ฉด ๋˜๊ฒ ๋„ค์š”!!

๐Ÿ“ Sudo code

์ด๋ฅผ ์ด์šฉํ•˜์—ฌ sudo code๋ฅผ ์งœ๋ณด๋ฉด

1๏ธโƒฃ ์ž…๋ ฅ์„ ๋ฐ›์•„์™€์„œ vector a์— ์ €์žฅํ•˜์ž
2๏ธโƒฃ DP-table์˜ ์ดˆ๊นƒ๊ฐ’์„ ์ €์žฅํ•˜์ž
    2๏ธโƒฃ-1 ์ด ๊ฒฝ์šฐ์—์„œ๋Š” DP[0]๊ฐ€ a[0]๊ฐ€ ๋˜๋ฉด ๋  ๊ฒƒ์ด๋‹ค. 
3๏ธโƒฃ ์ „์ฒด n๋ฒˆ์˜ ๊ฒฝ์šฐ์— ๋Œ€ํ•˜์—ฌ ๋‹ค์Œ์˜ ์กฐ๊ฑด์— ๋”ฐ๋ผ DP-table์„ ์ฑ„์›Œ๋„ฃ์ž. 
    3๏ธโƒฃ-1 dp[i] = max(dp[i - 1] + a[i], a[i])
4๏ธโƒฃ DP-table์— ์ €์žฅ๋œ ๊ฐ’ ์ค‘ ์ตœ๋Œ“๊ฐ’์„ ์ฐพ์•„ ์ถœ๋ ฅํ•œ๋‹ค. 

๐Ÿ–ฅ๏ธ ์ฝ”๋“œ

#include <iostream>
#include <vector>

using namespace std;

int max(int a, int b) {
    return a > b ? a : b;
}
int main() {
    int m = -2147483647;
    int n, tmp;

    cin >> n;
    vector<int> dp(n);
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    dp[0] = a[0];
    for (int i = 0; i < n; i++) {
        dp[i] = max(dp[i - 1] + a[i], a[i]);
        if (m < dp[i]) m = dp[i];
    }
    cout << m;
    return 0;
}

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

โœ”๏ธ DP๋ฅผ ์‰ฝ๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.
โœ”๏ธ ํ’€์ง€ ๋ชปํ–ˆ๋˜ ๋ฌธ์ œ๋Š” ์ตœ๋Œ€ํ•œ ๋น ๋ฅด๊ฒŒ ์™„์„ฑํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค...!!

Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET left a comment

Choose a reason for hiding this comment

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

DP๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋งค์šฐ ๊ฐ„๋‹จํ•œ ํ’€์ด๊ฐ€ ๋‚˜์˜ค๋„ค์š”!
์ €๋Š” ์ € ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ๋ฒฝ ๋Š๊ปด์„œ ํ’€์ง€ ๋ชปํ–ˆ๋Š”๋ฐ ํ•œ ์ˆ˜ ๋ฐฐ์šฐ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค..
์ž๋ฃŒ๊ตฌ์กฐ ๋๋‚ด๋ฉด ๊ฐ€์žฅ ๋จผ์ € DP๋ฅผ ๋ฐฐ์›Œ๋ณด๊ณ  ์‹ถ๋„ค์š”.

return a > b ? a : b;
}
int main() {
int m = -2147483647;
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ด๋ถ€๋ถ„์€

int m = INT32_MIN

์„ ์“ด๋‹ค๋ฉด ์ตœ์†Ÿ๊ฐ’์„ ๋ชฐ๋ผ๋„ intํ˜• ์ตœ์†Ÿ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”๊ฐ€ ๊ฐ€๋Šฅํ•œ๊ฑธ๋กœ ์••๋‹ˆ๋‹ค.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์˜ค ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๊ตฐ์š”!!
์ฐธ๊ณ ํ•ด์„œ ๋‹ค์Œ๋ฒˆ์—๋Š” ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿ˜

ํ˜น์‹œ INT32_MIN์„ ์“ธ๋ ค๋ฉด ๋”ฐ๋กœ ํ—ค๋”ํŒŒ์ผ์ด ํ•„์š”ํ•œ๊ฐ€์š”...?
์˜ˆ์ „์— C์—์„œ ์“ธ ๋•Œ์—๋Š” ํ•„์š”ํ–ˆ๋˜๊ฑฐ ๊ฐ™๊ธฐ๋„ ํ•œ๋ฐ...C++์—์„œ๋Š” iostream์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ์•„๋‹Œ์ง€๋ฅผ ๋ชจ๋ฅด๊ฒ ๋„ค์š”...

Copy link
Member

@kjs254 kjs254 left a comment

Choose a reason for hiding this comment

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

DP Table์ด ๋ญ”์ง€ ๋‹จ๋ฒˆ์— ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋ง ์ข‹์€ ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ˆ˜๊ธฐ๋กœ ์ง์ ‘ ํ’€์–ด๋ณด์•˜์„ ๋•Œ๋ž‘ ๋˜‘๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„์ด ๋˜๋Š” ์ ์ด ์ธ์ƒ ๊นŠ์—ˆ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ ๋‚œ์ด๋„๊ฐ€ ์‰ฌ์›Œ ๋ณด์—ฌ๋„ DP Table์„ ๋ชจ๋ฅธ๋‹ค๋ฉด ์กฐ๊ธˆ ํ—ค๋งฌ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@mong3125 mong3125 left a comment

Choose a reason for hiding this comment

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

1๏ธโƒฃ ์•ž์—์„œ ์ตœ๋Œ€์ธ ๊ฐ’์—์„œ n๋ฒˆ์งธ ์ˆ˜๋ฅผ ๋”ํ•œ ๊ฒƒ
2๏ธโƒฃ n๋ฒˆ์งธ ์ˆ˜

์ด๋Ÿฐ ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๋„ค์š”.
์‚ฝ์งˆํ•˜๋‹ค๊ฐ€ ์ด์ƒํ•˜๊ฒŒ ํ’€๊ณ  ํ•œ ์ˆ˜ ๋ฐฐ์šฐ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค ํ•˜ํ•ณ..

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