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

11-kangrae-jo #41

Merged
merged 2 commits into from
Jan 2, 2025
Merged

11-kangrae-jo #41

merged 2 commits into from
Jan 2, 2025

Conversation

kangrae-jo
Copy link
Collaborator

@kangrae-jo kangrae-jo commented Dec 21, 2024

πŸ”— 문제 링크

[ATM]

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

13m

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

μ‹œν—˜μ΄ 거의 λλ‚˜κ°€κ³  μ˜€λžœλ§Œμ— 감을 μž‘μ•„λ³Ό κ²Έ μ‰¬μš΄ 쑰금 μ‰¬μš΄ 문제둜 μ€€λΉ„ν–ˆμŠ΅λ‹ˆλ‹€.

이 문제λ₯Ό μš”μ•½ν•˜μžλ©΄ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

 ATM을 μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œ 쀄을 μ„œμžˆλ‹€.
 쀄을 μ„  μ‚¬λžŒλ“€μ΄ κΈ°λ‹€λ¦¬λŠ” μ‹œκ°„ ν•©μ˜ μ΅œμ†Œκ°’μ„ ꡬ해라.

μ΅œμ†Œ 값을 κ΅¬ν•˜κΈ° μœ„ν•΄μ„œλŠ” μš°μ„  ATM을 μ‚¬μš©ν•˜λŠ” μ‹œκ°„μ΄ 적은 μ‚¬λžŒ μˆœμ„œλŒ€λ‘œ 쀄을 μ„Έμ›Œμ•Ό ν•œλ‹€.
μ™œλƒν•˜λ©΄ μ‹œκ°„μ„ μ˜€λž˜μ“°λŠ” μ‚¬λžŒμ΄ μ•žμ— κ°„λ‹€λ©΄, μ „μ²΄μ μœΌλ‘œ κΈ°λ‹€λ¦¬λŠ” μ‹œκ°„μ΄ λŠ˜μ–΄λ‚˜κΈ° λ•Œλ¬Έμ΄λ‹€.

κ·Έ λ’€μ—λŠ” κ΅¬ν˜„μ˜ λ¬Έμ œμ΄λ‹€.
1번째 μ‚¬λžŒκ³Ό 2번째 μ‚¬λžŒ, .... λ§ˆμ§€λ§‰ μ‚¬λžŒκΉŒμ§€ 각각의 μ‚¬λžŒ i λŠ”
1 ~ i-1 μ‚¬λžŒμ΄ μ‚¬μš©ν•œ μ‹œκ°„λ§ŒνΌ κΈ°λ‹€λ €μ•Όν•œλ‹€.

λ”°λΌμ„œ resultμ—λŠ” 1 ~ i-1 μ‹œκ°„ + i μ‹œκ°„ (즉 1~i μ‹œκ°„)을 κ³„μ†ν•΄μ„œ λ”ν•΄μ£Όμ–΄μ•Όν•œλ‹€.

그것을 μ½”λ“œλ‘œ κ΅¬ν˜„ν•  수 μžˆλ‹€λ©΄ 이 λ¬Έμ œλŠ” μ‰½κ²Œ 톡과할 것이닀!

그런데... λ­”κ°€ λΉ„νš¨μœ¨ μ μ΄λΌλŠ” 생각이 쑰금 λ“€μ—ˆμŠ΅λ‹ˆλ‹€.
λ™μ‹œμ— λ­”κ°€ 이거 dp둜 ν’€ 수 μžˆμ„ κ²ƒλ§Œ 같은 λŠλ‚Œμ΄ λ“€μ—ˆμŠ΅λ‹ˆλ‹€.
κ·Έλž˜μ„œ ν•œ 번 ν’€μ–΄λ΄€μŠ΅λ‹ˆλ‹€~!

DP CODE
    vector<int> dp(N, 0);
    dp[0] = ATM[0];
    for (int i=1; i< N; i++){
        dp[i] += dp[i-1] + ATM[i];
    }

    int result = 0;
    for (int time : dp){
        result += time;
    }

    cout << result;

살짝 μ• λ§€ν•˜κΈ΄ν•˜μ§€λ§Œ 2쀑 for문을 κ³ μ³€λ‹€λŠ” 것에 μ˜λ―Έκ°€ μžˆλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.
κ°μ‚¬ν•©λ‹ˆλ‹€
μ‹œν—˜ λ§ˆμ§€λ§‰κΉŒμ§€ ν™”μ΄νŒ…~

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

.

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.

μ €λ²ˆμ— ν’€μ—ˆλ˜ λˆ„μ  ν•© λ¬Έμ œμ™€ λΉ„μŠ·ν•˜λ„€μš”. μ €λŠ” 이쀑 for문을 μ‚¬μš©ν•˜μ§€ μ•Šκ³  ν•œλ²ˆμ— ν’€μ–΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€. dpλ₯Ό μ‚¬μš©ν•œ μ½”λ“œλŠ” μΈμƒμ μ΄λ„€μš” κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€~~

sort(arr, arr + n);
for (int i = 0; i < n; i++) ret += arr[i] * (n - i);

@kokeunho
Copy link
Collaborator

저도 ν’€μ–΄λ³΄λ‹ˆ λ™μΌν•œ μ½”λ“œλ‘œ λ‚˜μ™”λ„€μš”
이번 ν•™κΈ° 수고 λ§ŽμœΌμ…¨μŠ΅λ‹ˆλ‹€!

Java code
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] time = new int[n];

        for (int i = 0; i < n; i++) {
            time[i] = sc.nextInt();
        }

        Arrays.sort(time);
        int result = time[0];

        for (int i = 1; i < n; i++) {
            time[i] = time[i] + time[i -1];
            result += time[i];
        }

        System.out.println(result);
    }
}

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.

λˆ„μ ν•©μ˜ λˆ„μ ν•©μ΄λΌ 동적배열 λΉ„μŠ·ν•˜κ²Œ ν’€λ©΄ 될것같닀고 μƒκ°ν–ˆμŠ΅λ‹ˆλ‹€.

λ‹€λ§Œ 수λ₯Ό μ •ν•΄μ£ΌκΈ°λ•Œλ¬Έμ— 동적은 μ•„λ‹ˆκ³  이름을 λΆ™μ΄μžλ©΄ 정적배열..?

n = int(input())

time = list(map(int, input().split()))

time.sort()
sum = 0

for i in range(len(time)-1):
    time[i+1] = time[i] + time[i+1]
    sum += time[i+1]

print(sum+time[0])

각 배열에 μ›μ†Œλ₯Ό μ΄μ „μ›μ†Œμ˜ λˆ„μ ν•©μœΌλ‘œ λ°”κΏ”μ£Όκ³ , λ°˜λ³΅λ¬Έλ§ˆλ‹€ sumλ³€μˆ˜μ— 합을 λ”ν•΄μ€λ‹ˆλ‹€.

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