๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’ป Computer Science/- Data Structure, Algorithm

[์•Œ๊ณ ๋ฆฌ์ฆ˜ PS(DP)] (๋ฐฑ์ค€ 9095) 1, 2, 3 ๋”ํ•˜๊ธฐ ์ž๋ฐ”

by Wonit 2020. 4. 1.
๋ฐฑ์ค€ 9095 ๋Š” solved.ac ๊ธฐ์ค€ Tier : silver 3

DP ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ ํฐ ๋ฌธ์ œ๋ฅผ ์ž‘์€ ๋ฌธ์ œ๋กœ ๋‚˜๋ˆ„๊ธฐ ์ด๋ฉฐ, ํ•ด๋‹น ๋ฌธ์ œ์—๋„ ์ ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€์–ด์•ผ ํ•œ๋‹ค.

ํฐ ๋ฌธ์ œ๋ฅผ ์ž‘์€ ๋ฌธ์ œ๋กœ ๋‚˜๋ˆ„๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฌธ์ œ์— ๋ณด์ด๋Š” ์ผ์ •ํ•œ ๊ทœ์น™์„ ์ฐพ์•„ ์ ํ™”์‹์œผ๋กœ ๋„์ถœํ•˜๋Š” ๊ณผ์ •์ด ํ•„์ˆ˜์ ์ด๋‹ค.

๋ฌธ์ œ

์ž…์ถœ๋ ฅ

๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •

๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ 1์— ๋”ํ•˜๊ธฐ ๊ฒฝ์šฐ์˜ ์ˆ˜์™€ 2์˜ ๋”ํ•˜๊ธฐ ๊ฒฝ์šฐ์˜ ์ˆ˜, 3์˜ ๋”ํ•˜๊ธฐ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋‚˜๋ˆ ์„œ ์ƒ๊ฐํ–ˆ๋‹ค.

์ž์„ธํžˆ ๋ฌธ์ œ๋ฅผ ๋ณธ๋‹ค๋ฉด x + x + x + x + x = n ์ผ ๊ฒฝ์šฐ

  • n์ด 1๋กœ ์ด๋ฃจ์–ด์ง„ ๊ฒฝ์šฐ

    x + x + x + x + 1 = n

    x ๋Š” n - 1

  • n์ด 2๋กœ ์ด๋ฃจ์–ด์ง„ ๊ฒฝ์šฐ

    x + x + x + x + 2 = n

    x ๋Š” n - 2

  • n์ด 3์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ฒฝ์šฐ

    x + x + x + x + 3 = n

    x ๋Š” n - 3

์œผ๋กœ ๋‚˜๋‰  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด d[n] ์„ ๊ตฌํ•˜๋ ค๋ฉด d[n-1] ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜ + d[n-2] ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜ + d[n-3] ์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํ•ฉํ•œ ๊ฒƒ์ด ๋  ๊ฒƒ์ด๋‹ค.

์ ํ™”์‹

์œ„์˜ ๋‚ด์šฉ์„ ์ ํ™”์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

d[n] = d[n-1] + d[n-2] + d[n-3] ์ด๋ผ๋Š” ์ ํ™”์‹์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.

์†Œ์Šค ์ฝ”๋“œ(์ž๋ฐ”)

์†Œ์Šค์ฝ”๋“œ๋ฅผ Top-Down, Bottom-Up ์œผ๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ž‘์„ฑํ•ด๋ณด์•˜๋‹ค.

Top Down

import java.io.*;

public class Main {

    static int[] memo = new int[12];

    public static void main(String[] args) throws IOException {

        BufferedReader input =  new BufferedReader(new InputStreamReader(System.in));

        BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));

        int t = Integer.parseInt(input.readLine());

        while(t-- > 0){

            int n = Integer.parseInt(input.readLine());

            output.write(dp(n) + "\n");
        }

        output.flush();

        output.close();

    }

    private static int dp(int n){

        if(memo[n] > 0) return memo[n];

        if(n == 1) return 1;

        else if(n == 2) return 2;

        else if(n == 3) return 4;

        else {

            memo[n] = dp(n-1) + dp(n-2) + dp(n-3);

            return memo[n];
        }
    }
}

Botton-Up

import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int t = Integer.parseInt(br.readLine());

        int[] arr;

        while(t-- > 0){

            int n = Integer.parseInt(br.readLine());

            arr = new int[12];

            arr[0] = 1;

            arr[1] = 2;

            arr[2] = 4;

            for (int i = 3; i < arr.length; i++) {

                arr[i] =  arr[i-3] + arr[i-2] + arr[i-1];

            }
            bw.write(arr[n-1] + "\n");
        }

        bw.flush();

        bw.close();
    }
}

๋Œ“๊ธ€