알고리즘 문제/백준

[백준, 프로그래머스] 11726번 문제. 2xN 타일링 - Java

Praetoriani 2021. 6. 29. 15:14

문제: https://www.acmicpc.net/problem/11726(백준)

문제: https://programmers.co.kr/learn/courses/30/lessons/12900(프로그래머스)

 

11726번: 2×n 타일링

2×n 크기의 직사각형을 1×2, 2×1 타일로 채우는 방법의 수를 구하는 프로그램을 작성하시오. 아래 그림은 2×5 크기의 직사각형을 채운 한 가지 방법의 예이다.

www.acmicpc.net

프로그래머스의 [멀리 뛰기] 문제와 사실상 같은 문제이다. 멀리 뛰기 게시글에 해당 내용을 정리 해 놓았다.

 

소스코드

import java.util.Scanner;

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

        long[] answers = new long[n + 1];
        answers[0] = 1;
        answers[1] = 1;

        for (int i = 2; i <= n; i++) {
            answers[i] = (answers[i - 1] + answers[i - 2]) % 10_007;
        }

        System.out.println(answers[n]);
    }
}
class Solution {
    public int solution(int n) {
        int[] answers = new int[n];
        answers[0] = 1;
        answers[1] = 2;
        for (int i = 2; i < n; i++) {
            answers[i] = (answers[i - 1] + answers[i - 2]) % 1000000007;
        }

        return answers[n - 1];
    }
}