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

2020. 4. 7. 15:50프로그래머스/LEVEL 2

import java.util.*;

class Solution {
    int solution(int[][] land) {
        int answer = 0;

        int[][] dp = new int[land.length][4];
        
        for(int i=0; i<dp[0].length; i++){
            dp[0][i] = land[0][i];
        }
        
        for(int i=1; i<dp.length; i++){
            for(int j=0; j<4; j++){
                int temp = beforeMax(i,j,dp);
                dp[i][j] = land[i][j] + temp;
            }
        }
        
        for(int i=0; i<4; i++){
            answer = Math.max(dp[dp.length-1][i],answer);
        }      
        
        return answer;
    }
    
    private int beforeMax(int x,int y,int[][] dp){
        
        int maxNum =0;
        for(int i=0; i<4; i++){
            if(i!=y){
            maxNum = Math.max(dp[x-1][i],maxNum);
            }
        }
        return maxNum;
    }
}