-
Notifications
You must be signed in to change notification settings - Fork 0
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
1-kangrae-jo #2
1-kangrae-jo #2
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λλΆμ μΉ΄λ°μΈ μκ³ λ¦¬μ¦
μ λ°°μ°κ³ κ°λλ€.
C++μ μ λͺ°λΌμ μ½λ 리뷰λ λͺ»νκ² μλλ€..
νμ΄μ¬μΌλ‘ νλ² μ§λ³΄μμ΅λλ€.
n = int(input())
num_list = list(map(int, input().split()))
sum = num_list[0]
max_sum = num_list[0]
for i in range(1, n):
sum = max(num_list[i], sum + num_list[i])
max_sum = max(max_sum, sum)
print(max_sum)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
κ³ μνμ ¨μ΅λλ€!! κΎΈμ€ν μ½λ 리뷰ν μ μμμΌλ©΄ ν΄μ π
int solution(vector<int>& s, int n) { | ||
// maxendpoint | ||
vector<int> dp(n + 1, 0); | ||
dp[1] = s[1]; | ||
|
||
int maxSum = dp[1]; | ||
for (int i = 2; i <= n; i++) { | ||
dp[i] = max(dp[i - 1] + s[i], s[i]); | ||
if (dp[i] > maxSum) maxSum = dp[i]; | ||
} | ||
|
||
return maxSum; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ μμ μΈ νμ΄λ€μ! μ¬μ€ O(n^2)λ μΆ©λΆνλ€κ³ ν μ μμ§λ§ λ λΉ λ₯΄κ³ ν¨μ¨μ μΈ λ°©λ²μ κ³ μνμ κ² λ무 μ’λ€μ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ΅λν©λ¬Έμ λ₯Ό μΉ΄λ°μμκ³ λ¦¬μ¦μΌλ‘ νμ΄μ¬μΌλ‘ νμλ κΈ°μ΅μ΄ λ©λλ€. κ°μ O(n)μκ³ λ¦¬μ¦μ΄κΈ°λ νκ³ λ³μλͺ , μ½λ μ λΆ ν μ‘μκ³³ μμ΄λ³΄μ λλ€.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ΅λν©λ¬Έμ λ₯Ό μΉ΄λ°μμκ³ λ¦¬μ¦μΌλ‘ νμ΄μ¬μΌλ‘ νμλ κΈ°μ΅μ΄ λ©λλ€. κ°μ O(n)μκ³ λ¦¬μ¦μ΄κΈ°λ νκ³ λ³μλͺ , μ½λ μ λΆ ν μ‘μκ³³ μμ΄λ³΄μ λλ€.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μκ³ νμ ¨μ΅λλ€!
int solution(vector<int>& s, int n) { | ||
// maxendpoint | ||
vector<int> dp(n + 1, 0); | ||
dp[1] = s[1]; | ||
|
||
int maxSum = dp[1]; | ||
for (int i = 2; i <= n; i++) { | ||
dp[i] = max(dp[i - 1] + s[i], s[i]); | ||
if (dp[i] > maxSum) maxSum = dp[i]; | ||
} | ||
|
||
return maxSum; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ΅λν©λ¬Έμ λ₯Ό μΉ΄λ°μμκ³ λ¦¬μ¦μΌλ‘ νμ΄μ¬μΌλ‘ νμλ κΈ°μ΅μ΄ λ©λλ€. κ°μ O(n)μκ³ λ¦¬μ¦μ΄κΈ°λ νκ³ λ³μλͺ , μ½λ μ λΆ ν μ‘μκ³³ μμ΄λ³΄μ λλ€.
π λ¬Έμ λ§ν¬
https://www.acmicpc.net/problem/1912
βοΈ μμλ μκ°
30m
β¨ μλ μ½λ
λ¬Έμ μ€λͺ
쑰건
νμ΄
Β 'μ°μλ λͺκ°μ μμ ν©'λΌλ λ§μ λμ κ³νλ²μ΄ λ μ¬λλ€.
Β λ§μ½ μ°μλ 1κ°μ ν©, 2κ°μ ν©, 3κ°μ ν©, ... , nκ°μ ν©μ λͺ¨λ ꡬνκ³ λΉκ΅νλ€λ©΄
O(n^2)μ μκ°μ΄ νμν κ²μ΄λ€. (μ°λ¦¬λ μ΄λ° μκ°λ³΅μ‘λλ₯Ό μνμ§ μλλ€.)
Β μ λ ₯λ°μ μμ΄μ 1ν μννλ©° μ°μλ μ΅λ ν©μ ꡬν μ μμ κ² κ°μλ€.
Β μ½ 30λΆ κ³ λ―Όν΄λ³Έ κ²°κ³Ό λ°°μ΄μ μμ°¨μ μΌλ‘ μννλ©° 'μ΄μ λΆλΆ ν©μ νμ¬ μμλ₯Ό λν κ°'κ³Ό 'νμ¬ μμ λ¨λ 'κ° μ€ ν° κ²μ μ ννκ³
μ§κΈκΉμ§μ μ΅λ ν©(maxSum)κ³Ό λΉκ΅νλ λ°©μμΌλ‘ μ΅μ’ μ μΌλ‘ μ°μλ μ΅λ ν©μ ꡬν μ μμλ€.
μλμ½λ
3-1. 'μ΄μ λΆλΆ ν©μ νμ¬ μμλ₯Ό λν κ°'κ³Ό 'νμ¬ μμ'λ₯Ό λΉκ΅νμ¬ ν° κ°μ dp[i]λ‘ μ€μ
3-2. dp[i]κ° μ 체 maxSumλ³΄λ€ ν¬λ©΄ dp[i]λ‘ κ°±μ
π μλ‘κ² μκ²λ λ΄μ©
Β μ²μμλ 2λ² κ³Όμ μμ dp[1] = max(-1001, s[1])μ νμλλ° κ·Έλ΄ νμκ° μμλ€. μμ΄μ μλ μ΄λ―Έ -1000μ΄μμ΄λΌλ μ‘°κ±΄μ΄ μκΈ° λλ¬Έμ΄λ€.
Β κ·Έλ¦¬κ³ λ°°μ΄μ μ°μ§ μκ³ λ λ¬Έμ λ₯Ό ν μ μμ κ² κ°μλ€. μλνλ©΄ dpλ°°μ΄μ μ§μ κ°κ³Ό νμ¬ κ° λ§μ μ¬μ©νκΈ° λλ¬Έμ΄λ€.
μμ λ μλμ½λλ λ€μκ³Ό κ°λ€.
2-1. currentSumμ s[i]λ₯Ό λν κ°κ³Ό s[i]λ¨λ κ° μ€ λ ν° κ°μ currentSumμΌλ‘ ν¨
2-2. currentSum > maxSumμ΄λ©΄ maxSumκ° κ°±μ
Β μ‘°κΈμ΄μ§λ§ λ°°μ΄μ μ¬μ©νλ λ°©λ²λ³΄λ€ λ©λͺ¨λ¦¬λ₯Ό μ μ½ν μ μλ€λ μ₯μ μ΄ μλ€κ³ μκ°νλ€.
Β λ§μ§λ§μΌλ‘ μ΄ λ¬Έμ μ λμ΄λλ₯Ό μμλ³΄κ³ μΆμλ€. λ°±μ€ μ€λ²2 μ λμ λμ΄λμ΄λ©° kadene's algorithmμΌλ‘ μ μλ €μ§ λ¬Έμ λΌκ³ νλ€.
Β μμ§ μκ³ λ¦¬μ¦ μ΄λ³΄μΈ λμκ²λ 40~50λΆ μ λ μκ°νλ©΄ λ΅μ΄ λμ€λ λ¬Έμ μ λμ μμ€μ΄λΌκ³ μκ°νλ€.
Β νμ§λ§ μ΄ λ¬Έμ λ μ§λ νκΈ° μκ³ λ¦¬μ¦ μμ μμ λ€λ£¨μλ λ΄μ©μ΄λΌ νμ΄λ²μ΄ λ μ¬λΌ μ‘°κΈ λ 빨리 ν μ μμλ€.