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

Taesun Lee·2020년 12월 21일
0

algorithm-study

목록 보기
10/10

solution

class Solution {
    int solution(int[][] land) {
        int answer = 0;
        
        int x_length = land.length;
        int max = land[0][0];
        int nextMax = 0;

        for(int i=1; i<4; i++) {
            if(land[0][i] > max) {
                nextMax = max;
                max = land[0][i];
            } else if(land[0][i] > nextMax) {
                nextMax = land[0][i];
            }
        }
        
        if(x_length == 1) {
            return max;
        }
        
        for(int i=1; i<x_length; i++) {
            int tempMax = max;
            int tempNextMax = nextMax;
            
            for(int j=0; j<4; j++) {
                if(land[i-1][j] == tempMax) {
                    land[i][j] += tempNextMax;
                } else {
                    land[i][j] += tempMax;
                }
                
                if(j == 0) {
                    max = land[i][j];
                    nextMax = 0;
                } else {
                    if(land[i][j] > max) {
                        nextMax = max;
                        max = land[i][j];
                    } else {
                        if(land[i][j] > nextMax) {
                            nextMax = land[i][j];
                        }
                    }
                }
            }
        }

        return max;
    }
}
profile
구름위 개발자

0개의 댓글