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

1-kangrae-jo #2

Merged
merged 3 commits into from
Oct 1, 2024
Merged

1-kangrae-jo #2

merged 3 commits into from
Oct 1, 2024

Conversation

kangrae-jo
Copy link
Collaborator

πŸ”— 문제 링크

https://www.acmicpc.net/problem/1912

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

30m

✨ μˆ˜λ„ μ½”λ“œ

λ¬Έμ œμ„€λͺ…

  1. n개의 μ •μˆ˜λ‘œ 이루어진 μˆ˜μ—΄μ΄ 주어진닀.
  2. κ·Έ 쀑 μ—°μ†λœ λͺ‡κ°œμ˜ 수λ₯Ό μ„ νƒν•΄μ„œ ꡬ할 수 μžˆλŠ” ν•© 쀑 κ°€μž₯ 큰 합을 κ΅¬ν•˜λŠ” λ¬Έμ œμ΄λ‹€.

쑰건

  1. ν•œ 개 μ΄μƒμ˜ 수λ₯Ό μ„ νƒν•΄μ•Όν•œλ‹€.
  2. 1 <= n <= 100,000
  3. μˆ˜μ—΄μ˜ 수 λ²”μœ„ -1,000 ~ 1,000

풀이

Β 'μ—°μ†λœ λͺ‡κ°œμ˜ 수의 ν•©'λΌλŠ” 말에 λ™μ κ³„νšλ²•μ΄ λ– μ˜¬λžλ‹€.

Β λ§Œμ•½ μ—°μ†λœ 1개의 ν•©, 2개의 ν•©, 3개의 ν•©, ... , n개의 합을 λͺ¨λ‘ κ΅¬ν•˜κ³  λΉ„κ΅ν•œλ‹€λ©΄
O(n^2)의 μ‹œκ°„μ΄ ν•„μš”ν•  것이닀. (μš°λ¦¬λŠ” 이런 μ‹œκ°„λ³΅μž‘λ„λ₯Ό μ›ν•˜μ§€ μ•ŠλŠ”λ‹€.)

Β μž…λ ₯받은 μˆ˜μ—΄μ„ 1회 μˆœνšŒν•˜λ©° μ—°μ†λœ μ΅œλŒ€ 합을 ꡬ할 수 μžˆμ„ 것 κ°™μ•˜λ‹€.

Β μ•½ 30λΆ„ κ³ λ―Όν•΄λ³Έ κ²°κ³Ό 배열을 순차적으둜 μˆœνšŒν•˜λ©° '이전 λΆ€λΆ„ 합에 ν˜„μž¬ μ›μ†Œλ₯Ό λ”ν•œ κ°’'κ³Ό 'ν˜„μž¬ μ›μ†Œ 단독'κ°’ 쀑 큰 것을 μ„ νƒν•˜κ³ 
μ§€κΈˆκΉŒμ§€μ˜ μ΅œλŒ€ ν•©(maxSum)κ³Ό λΉ„κ΅ν•˜λŠ” λ°©μ‹μœΌλ‘œ μ΅œμ’…μ μœΌλ‘œ μ—°μ†λœ μ΅œλŒ€ 합을 ꡬ할 수 μžˆμ—ˆλ‹€.

μˆ˜λ„μ½”λ“œ

  1. dpλ°°μ—΄μ˜ 길이λ₯Ό n+1둜 μƒμ„±ν•˜κ³ , λͺ¨λ“  μ›μ†Œλ₯Ό 0으둜 μ΄ˆκΈ°ν™” (0번 인덱슀 μ‚¬μš©x)
  2. dp[1]κ³Ό maxSumλ³€μˆ˜λ₯Ό s[1]으둜 μ΄ˆκΈ°ν™”
  3. i에 λŒ€ν•΄μ„œ 2λΆ€ν„° nκΉŒμ§€ 반볡
    3-1. '이전 λΆ€λΆ„ 합에 ν˜„μž¬ μ›μ†Œλ₯Ό λ”ν•œ κ°’'κ³Ό 'ν˜„μž¬ μ›μ†Œ'λ₯Ό λΉ„κ΅ν•˜μ—¬ 큰 값을 dp[i]둜 μ„€μ •
    3-2. dp[i]κ°€ 전체 maxSum보닀 크면 dp[i]둜 κ°±μ‹ 
   for (int i = 2; i <= n; i++) {
        dp[i] = max(dp[i - 1] + s[i], s[i]);
        if (dp[i] > maxSum) maxSum = dp[i];
    }

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

Β μ²˜μŒμ—λŠ” 2번 κ³Όμ •μ—μ„œ dp[1] = max(-1001, s[1])을 ν–ˆμ—ˆλŠ”λ° 그럴 ν•„μš”κ°€ μ—†μ—ˆλ‹€. μˆ˜μ—΄μ˜ μˆ˜λŠ” 이미 -1000μ΄μƒμ΄λΌλŠ” 쑰건이 있기 λ•Œλ¬Έμ΄λ‹€.

 그리고 배열을 쓰지 μ•Šκ³ λ„ 문제λ₯Ό ν’€ 수 μžˆμ„ 것 κ°™μ•˜λ‹€. μ™œλƒν•˜λ©΄ dpλ°°μ—΄μ˜ 직전 κ°’κ³Ό ν˜„μž¬ κ°’ λ§Œμ„ μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€.
μˆ˜μ •λœ μˆ˜λ„μ½”λ“œλŠ” λ‹€μŒκ³Ό κ°™λ‹€.

  1. maxSumλ³€μˆ˜μ™€ currentSumλ³€μˆ˜λ₯Ό s[0]으둜 μ΄ˆκΈ°ν™”
  2. i에 λŒ€ν•΄μ„œ 1λΆ€ν„° n-1κΉŒμ§€ 반볡
    2-1. currentSum에 s[i]λ₯Ό λ”ν•œ κ°’κ³Ό s[i]단독 κ°’ 쀑 더 큰 값을 currentSum으둜 함
    2-2. currentSum > maxSum이면 maxSumκ°’ κ°±μ‹ 

Β μ‘°κΈˆμ΄μ§€λ§Œ 배열을 μ‚¬μš©ν•˜λŠ” 방법보닀 λ©”λͺ¨λ¦¬λ₯Ό μ ˆμ•½ν•  수 μžˆλ‹€λŠ” μž₯점이 μžˆλ‹€κ³  μƒκ°ν•œλ‹€.

  for (int i = 1; i < n; i++) {
        currentSum = max(currentSum + s[i], s[i]);
        if (currentSum > maxSum) maxSum = currentSum;
    }

Β λ§ˆμ§€λ§‰μœΌλ‘œ 이 문제의 λ‚œμ΄λ„λ₯Ό μ•Œμ•„λ³΄κ³  μ‹Άμ—ˆλ‹€. λ°±μ€€ 싀버2 μ •λ„μ˜ λ‚œμ΄λ„μ΄λ©° kadene's algorithm으둜 잘 μ•Œλ €μ§„ 문제라고 ν•œλ‹€.
 아직 μ•Œκ³ λ¦¬μ¦˜ 초보인 λ‚˜μ—κ²ŒλŠ” 40~50λΆ„ 정도 μƒκ°ν•˜λ©΄ 닡이 λ‚˜μ˜€λŠ” 문제 μ •λ„μ˜ μˆ˜μ€€μ΄λΌκ³  μƒκ°ν•œλ‹€.
Β ν•˜μ§€λ§Œ 이 λ¬Έμ œλŠ” μ§€λ‚œ ν•™κΈ° μ•Œκ³ λ¦¬μ¦˜ μˆ˜μ—…μ—μ„œ λ‹€λ£¨μ—ˆλ˜ λ‚΄μš©μ΄λΌ 풀이법이 λ– μ˜¬λΌ 쑰금 더 빨리 ν’€ 수 μžˆμ—ˆλ‹€.

Copy link
Collaborator

@kokeunho kokeunho left a 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)

@kangrae-jo kangrae-jo changed the title 2-kangrae-jo 1-kangrae-jo Oct 1, 2024
Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

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

κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€!! κΎΈμ€€νžˆ μ½”λ“œ 리뷰할 수 μžˆμ—ˆμœΌλ©΄ ν•΄μš” πŸ™Œ

Comment on lines +7 to +19
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;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

정석적인 ν’€μ΄λ„€μš”! 사싀 O(n^2)도 μΆ©λΆ„ν•˜λ‹€κ³  ν•  수 μžˆμ§€λ§Œ 더 λΉ λ₯΄κ³  효율적인 방법을 κ³ μ•ˆν•˜μ‹ κ²Œ λ„ˆλ¬΄ μ’‹λ„€μš”

Copy link
Collaborator

Choose a reason for hiding this comment

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

μ΅œλŒ€ν•©λ¬Έμ œλ₯Ό μΉ΄λ°μ•ˆμ•Œκ³ λ¦¬μ¦˜μœΌλ‘œ 파이썬으둜 ν’€μ—ˆλ˜ 기얡이 λ‚©λ‹ˆλ‹€. 같은 O(n)μ•Œκ³ λ¦¬μ¦˜μ΄κΈ°λ„ ν•˜κ³  λ³€μˆ˜λͺ…, μ½”λ“œ μ „λΆ€ ν μž‘μ„κ³³ μ—†μ–΄λ³΄μž…λ‹ˆλ‹€.

Copy link
Collaborator

@wnsmir wnsmir left a comment

Choose a reason for hiding this comment

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

μ΅œλŒ€ν•©λ¬Έμ œλ₯Ό μΉ΄λ°μ•ˆμ•Œκ³ λ¦¬μ¦˜μœΌλ‘œ 파이썬으둜 ν’€μ—ˆλ˜ 기얡이 λ‚©λ‹ˆλ‹€. 같은 O(n)μ•Œκ³ λ¦¬μ¦˜μ΄κΈ°λ„ ν•˜κ³  λ³€μˆ˜λͺ…, μ½”λ“œ μ „λΆ€ ν μž‘μ„κ³³ μ—†μ–΄λ³΄μž…λ‹ˆλ‹€.

Copy link
Collaborator

@wnsmir wnsmir left a comment

Choose a reason for hiding this comment

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

μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€!

Comment on lines +7 to +19
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;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

μ΅œλŒ€ν•©λ¬Έμ œλ₯Ό μΉ΄λ°μ•ˆμ•Œκ³ λ¦¬μ¦˜μœΌλ‘œ 파이썬으둜 ν’€μ—ˆλ˜ 기얡이 λ‚©λ‹ˆλ‹€. 같은 O(n)μ•Œκ³ λ¦¬μ¦˜μ΄κΈ°λ„ ν•˜κ³  λ³€μˆ˜λͺ…, μ½”λ“œ μ „λΆ€ ν μž‘μ„κ³³ μ—†μ–΄λ³΄μž…λ‹ˆλ‹€.

@kangrae-jo kangrae-jo merged commit 8a261a4 into main Oct 1, 2024
1 check passed
@kangrae-jo kangrae-jo deleted the 1-kangrae-jo branch October 28, 2024 04:13
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