상세 컨텐츠

본문 제목

[프로그래머스] 땅따먹기

💯ProblemSolving/문제 풀이-Java

by :부셔져버린개발자 2025. 2. 4. 13:31

본문

       문제 요약        

- 문제

https://school.programmers.co.kr/learn/courses/30/lessons/12913

         아이디어        

dp

 

 

         소스코드        

class Solution {
    static int ROW, COL;
    static int answer = 0;
    static int[][] box;
    int solution(int[][] land) {
        ROW = land.length;
        COL = land[0].length;
        box = land;

        int[][] dp = new int[ROW][COL];
        for(int c = 0; c < COL; c++){
            dp[0][c] = box[0][c];
        }
        
        for(int r = 1; r < ROW; r++) {
            for(int c = 0; c < COL; c++){
                for(int j = 0; j < COL; j++){
                    if(c==j)continue;
                    dp[r][c] = Math.max(box[r][c] + dp[r-1][j], dp[r][c]);
                }
            }
        }
        
        for(int c = 0; c < COL; c++) {
            answer = Math.max(dp[ROW-1][c], answer);    
        }

        return answer;
    }

}

 

 

728x90

관련글 더보기