Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

csct3434

[level 2] 땅따먹기 - 12913 본문

프로그래머스

[level 2] 땅따먹기 - 12913

csct3434 2024. 2. 28. 19:50

문제 링크

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

class Solution {

    int solution(int[][] land) {
       int rows = land.length;
       int cols = land[0].length;
       int[][] dp = new int[rows][cols];

       init(land, dp, cols);

       for (int x = 1; x < rows; x++) {
          for (int y = 0; y < cols; y++) {
             dp[x][y] = land[x][y] + calcMax(dp, x - 1, cols, y);
          }
       }

       return calcMax(dp, rows - 1, cols, -1);
    }

    private void init(int[][] land, int[][] dp, int cols) {
       for (int i = 0; i < cols; i++) {
          dp[0][i] = land[0][i];
       }
    }

    private int calcMax(int[][] dp, int targetRow, int cols, int blockedCol) {
       int max = 0;

       for (int col = 0; col < cols; col++) {
          if (col != blockedCol) {
             max = Math.max(max, dp[targetRow][col]);
          }
       }

       return max;
    }
}