[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] LV.2 ๋•…๋”ฐ๋จน๊ธฐ (์ž๋ฐ” java)

 

1. ๋ฌธ์ œ : https://school.programmers.co.kr/learn/courses/30/lessons/12913

 

2. ํ’€์ด

์™„์ „ํƒ์ƒ‰์œผ๋กœ ํ’€์—ˆ์ง€๋งŒ ์‹œ๊ฐ„์ดˆ๊ณผ

dp๋กœ ์ด์ „ ํ–‰์—์„œ์˜ ์ตœ๋Œ“๊ฐ’์„ ํ•ด๋‹น ํ–‰์— ๋”ํ•ด์ฃผ๋Š” ์‹์œผ๋กœ dp ๊ฐ’ ์—…๋ฐ์ดํŠธํ•˜๋ฉฐ ๋งˆ์ง€๋ง‰์— ๋น„๊ตํ•˜๋Š” ํ’€์ด

 

3. ์ฝ”๋“œ

class Solution {
    int solution(int[][] land) {
        int answer = 0;
        for(int i=1; i<land.length; i++){
            land[i][0] += Math.max(land[i-1][1], Math.max(land[i-1][2], land[i-1][3]));
            land[i][1] += Math.max(land[i-1][0], Math.max(land[i-1][2], land[i-1][3]));
            land[i][2] += Math.max(land[i-1][0], Math.max(land[i-1][1], land[i-1][3]));
            land[i][3] += Math.max(land[i-1][0], Math.max(land[i-1][1], land[i-1][2]));
        }
        
        for(int i=0; i<4; i++){
            answer = Math.max(answer, land[land.length-1][i]);
        }
        
        return answer;
    }
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ