Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

코딩응급실

프로그래머스: 2 x n 타일링 본문

Java

프로그래머스: 2 x n 타일링

Daeryuk Kim 2024. 3. 30. 21:47
import java.util.*;
public class Solution {
    public int solution(int n) {
        if (n == 1) return 1;
        if (n == 2) return 2;
        
        int mod = 1_000_000_007; //계산 과정에서 나오는 모든 값은 1,000,000,007로 나눈 나머지를 사용하여 오버플로우를 방지합니다.
        int[] dp = new int[n + 1];
        
        dp[1] = 1;
        dp[2] = 2;
        
        for (int i = 3; i <= n; i++) {
            dp[i] = (dp[i - 1] + dp[i - 2]) % mod;
        }
        
        return dp[n];
    }
    public static void main(String[] args) {
        Solution sol = new Solution();
        int n = 7; 
        
        System.out.println(sol.solution(n));
    }
}