본문 바로가기
알고리즘/📌leetcode

Unique Paths - #dp

by IMSfromSeoul 2021. 12. 3.

📌 문제

  • 길 경우의 수 문제

https://leetcode.com/problems/unique-paths/

📌 문제 풀이

📌 코드

public class Main{
    public static void main(String[] args){
        int m=5;
        int n=5;

        Solution solution = new Solution();
        solution.uniquePaths(m,n);
    }
    static class Solution{
        public int uniquePaths(int N,int M){
            int[][] map = new int[N][M];

            for(int i=0;i<N;i++){
                map[i][0] = 1;
            }

            for(int j=0;j<M;j++){
                map[0][j] = 1;
            }

            for(int i=1;i<N;i++){
                for(int j=1;j<M;j++){
                    map[i][j] = map[i-1][j] + map[i][j-1];
                }
            }

            for(int i=0;i<N;i++){
                for(int j=0;j<M;j++){
                    System.out.print(map[i][j] + " ");
                }
                System.out.println();
            }
            return map[N-1][M-1];
        }
    }
}

'알고리즘 > 📌leetcode' 카테고리의 다른 글

Climbing Stairs - #dp  (0) 2021.12.03
Course Schedule - 위상정렬  (0) 2021.11.30
Word Ladder  (0) 2021.11.30
Max Area of Island  (0) 2021.11.30
Maximum Depth of Binary Tree  (0) 2021.11.30

댓글