Skip to content

Commit 59687f3

Browse files
committed
1699 - 제곱수의 합 (DP, 풀이 참조)
DP는 점화식만 잘 찾아 세우면 쉽게 풀린다. D[N] = 필요한 항의 개수 a^2 + b^2 + c ^2+ ... + y^2 + z^2 = N -> D[N] = a~z 개수 인데, subproblem으로 작게 하는방법? a^2 + b^2 + c ^2+ ... + y^2 = N - z^2 -> D[N-z^2] =a~z 개수 - 1 z는 뭐일지 모름.. 단, 1 <= z^2 <= N 이어야함 변수로 두고 가능한거 다 돌면서 적절한 값(여기선 최소값)을 찾으면 됨 <- ( O(N^(1/2)) 소요 )
1 parent 625e320 commit 59687f3

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

1699.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
7+
Scanner sc = new Scanner(System.in);
8+
9+
int n = sc.nextInt();
10+
11+
int[] d = new int[n+1];
12+
d[1] = 1;
13+
14+
for(int i=2; i<n+1; i++) {
15+
16+
int x = 1;
17+
int min = Integer.MAX_VALUE;
18+
while(i-x*x >=0) {
19+
min = Math.min(min, d[i-x*x]);
20+
x++;
21+
}
22+
d[i] = min + 1;
23+
}
24+
System.out.println(d[n]);
25+
}
26+
}

0 commit comments

Comments
 (0)