백준 GOLD 5(알약) - #4811

image

  • 사용 언어 : javascript

  • 해결 날짜 : 2023-02-13

  • 해결 방법 :

    • dp를 활용해 문제 해결
    • dp[i][j] = dp[i-1][j] + dp[i][j-1]
  • 회고 :

    • x
  • 코드

    const fs = require('fs');
    const filePath = process.platform === 'linux' ? '/dev/stdin' : '../input.txt';
    const input = fs.readFileSync(filePath).toString().trim();
    
    solution(input);
    
    function solution(props) {
      props = props.split('\n');
      const pills = props.slice(0, props.length - 1).map((e) => +e);
      const dp = Array.from({ length: 31 }, (_, i) => Array(31).fill(i === 0 ? 1 : 0));
    
      for (let i = 1; i <= 30; i++) {
        for (let j = i; j <= 30; j++) {
          dp[i][j] += dp[i - 1][j] + dp[i][j - 1];
        }
      }
    
      pills.forEach((pill) => {
        console.log(dp[pill][pill]);
      });
    }
    
  • 출처: 백준