Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion caucsejunseo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@
| 9μ°¨μ‹œ | 2025.04.30 | λ¬Έμžμ—΄ | [λ“£λ³΄μž‘](https://www.acmicpc.net/problem/1764)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/35|
|10μ°¨μ‹œ| 2025.05.05 | DP | [1λ‘œλ§Œλ“€κΈ°](https://www.acmicpc.net/problem/1463)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/38|
|11μ°¨μ‹œ| 2025.05.07 | DP | [1,2,3λ”ν•˜κΈ°](https://www.acmicpc.net/problem/9095)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/41|

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX 1000001


int main() {

int N = 0;
scanf("%d", &N);

int* dp = (int*)malloc(sizeof(int) * MAX);
Copy link
Contributor

Choose a reason for hiding this comment

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

malloc은 동적 λ©”λͺ¨λ¦¬ 할당에 μ‚¬μš©ν•˜λŠ” ν•¨μˆ˜μΈλ°, MAX 값을 μƒμˆ˜λ‘œ 정해놓고 이λ₯Ό 기반으둜 ν• λ‹Ήν•˜λŠ” 것은 사싀상 정적 λ©”λͺ¨λ¦¬ ν• λ‹Ήκ³Ό λ‹€λ₯Ό λ°” μ—†μ–΄ λ³΄μ΄λ„€μš”..! 정적 λ©”λͺ¨λ¦¬ ν• λ‹ΉμœΌλ‘œλ„ μΆ©λΆ„νžˆ ν•΄κ²°ν•  수 μžˆλŠ” λ¬Έμ œλΌμ„œ λ‹€μŒκ³Ό 같이 μˆ˜μ •ν•˜λ©΄ 훨씬 κ°„λ‹¨ν•œ μ½”λ“œκ°€ 될 것 κ°™μŠ΅λ‹ˆλ‹€.

Suggested change
int* dp = (int*)malloc(sizeof(int) * MAX);
int dp[1000001];

Copy link
Member

@9kyo-hwang 9kyo-hwang May 7, 2025

Choose a reason for hiding this comment

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

μ²¨μ–Έν•˜μžλ©΄, ν•¨μˆ˜ λ‚΄μ—μ„œ 큰 μ‚¬μ΄μ¦ˆμ˜ 배열을 μ„ μ–Έν•˜κ²Œ 될 경우 Stack λ©”λͺ¨λ¦¬ μ΄ˆκ³Όκ°€ λ°œμƒν•  수 μžˆμŠ΅λ‹ˆλ‹€. κ³ μ •λœ 큰 크기의 배열을 μ„ μ–Έν•˜κ³  μ‹ΆμœΌλ©΄ μ „μ—­ λ³€μˆ˜λ‘œ λ‘λŠ” 게 μ’‹μŠ΅λ‹ˆλ‹€ :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ν™•μ‹€νžˆ κ·Έλ ‡λ„€μš”!
μš”μ¦˜ 무쑰건적으둜 정적 할당을 ν•˜κ³  μžˆλŠ” 데, μ’€ 더 μ½”λ“œλ₯Ό μ“Έ λ•Œ μƒκ°ν•˜κ³ , 상황에 맞게 써봐야 ν•  κ±° κ°™μŠ΄λ‹€!


if (dp == NULL) {
printf("λ©”λͺ¨λ¦¬ ν• λ‹Ή μ‹€νŒ¨!\n");
return 1;
}
Comment on lines +15 to +18
Copy link
Contributor

Choose a reason for hiding this comment

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

이 μ½”λ“œλŠ” 동적 λ©”λͺ¨λ¦¬ ν• λ‹Ή μ‹€νŒ¨μ— λŒ€λΉ„ν•˜κΈ° μœ„ν•΄μ„œ μž‘μ„±ν•˜μ‹  것 같은데, 이 κ²½μš°μ—λŠ” ν• λ‹Ή 크기가 κ³ μ •λœ μƒμˆ˜(MAX)이고 크기도 비ꡐ적 μž‘μ•„μ„œ 일반적인 ν™˜κ²½μ—μ„œλŠ” ν• λ‹Ή μ‹€νŒ¨κ°€ λ°œμƒν•  κ°€λŠ₯성이 거의 μ—†λ‹€κ³  봐도 λ©λ‹ˆλ‹€. λ°±μ€€μ—μ„œλŠ” μ—₯κ°„ν•΄μ„œ λ©λ‹ˆλ©. μ§€μ›Œλ„ 무방할 것 κ°™λ„€μš”.


dp[1] = 0;

for (int i = 2; i < N + 1; i++)
{
dp[i] = dp[i - 1] + 1;

if (i % 3 == 0)
{
dp[i] = MIN(dp[i], dp[i / 3] + 1);
}
if (i % 2 == 0)
{
dp[i] = MIN(dp[i], dp[i / 2] + 1);
}
}

printf("%d", dp[N]);

return 0;
}