LeetCode 풀기 - 62. Unique Paths

백지우·2026년 9월 23일

LeetCode

목록 보기
67/76
  • LeetCode 최소 하루에 하나는 풀자는 마인드
  • 사용언어: JAVA

62. Unique Paths

풀이

  • 방법: dp를 각 칸까지 도달할 수 있는 방법의 수로 잡아 각각 위 칸 + 왼쪽 칸의 방법의 수로 계산
class Solution {
    public int uniquePaths(int m, int n) {
        int[][] dp = new int[m][n];

        Arrays.fill(dp[0], 1);

        for(int i = 1; i < m; i++){
            for(int j = 0; j < n; j++){
                if(j == 0) dp[i][j] = 1;
                else{
                    dp[i][j] = dp[i-1][j] + dp[i][j-1];
                }
            }
        }


        return dp[m-1][n-1];
    }
}

0개의 댓글