백준 17626 java : DP

magicdrill·2025년 2월 3일
0

백준 문제풀이

목록 보기
542/654

백준 17626 java : DP

import java.util.Scanner;

public class bj17626 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N;

        N = sc.nextInt();
        System.out.println(findAnswer(N));

        sc.close();
    }

    public static int findAnswer(int N) {
        int min, temp;
        int [] DP = new int[N+1];
        int i, j;

        DP[1] = 1;
        for(i = 2; i <= N; i++) {
            min = Integer.MAX_VALUE;
            for(j = 1; j * j <= i; j++) {
                temp = i - j * j;
                min = Math.min(min, DP[temp]);
            }
            DP[i] = min + 1;
            System.out.print(DP[i] + " ");
        }
        System.out.println();

        return DP[N];
    }
}

0개의 댓글