백준 SILVER 1(오르막 수)

image

  • 사용 언어 : javascript

  • 해결 날짜 : 2023-02-02

  • 해결 방법 :

    • dp를 활용해 문제 해결
    • dp[i][j] = dp[i][j - 1] + dp[i - 1][j]임을 이용하여 dp 업데이트
  • 회고 :

    • 10007로 나눈 나머지 반환
  • 코드

    const fs = require('fs');
    const filePath = process.platform === 'linux' ? '/dev/stdin' : '../input.txt';
    const input = fs.readFileSync(filePath).toString().trim();
    
    solution(input);
    
    function solution(prop) {
      const N = +prop;
      const dp = Array.from({ length: N }, () => Array(10).fill(1));
    
      for (let i = 1; i < N; i++) {
        for (let j = 1; j < 10; j++) {
          dp[i][j] = (dp[i][j - 1] + dp[i - 1][j]) % 10007;
        }
      }
    
      console.log(dp[N - 1].reduce((acc, cur) => acc + cur, 0) % 10007);
    }
    
  • 출처: 백준