Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.07.06 - [PG] 땅따먹기 #112

Closed
suhyunsim opened this issue Jul 7, 2021 · 0 comments
Closed

21.07.06 - [PG] 땅따먹기 #112

suhyunsim opened this issue Jul 7, 2021 · 0 comments
Assignees
Labels
DP 다이나믹 프로그래밍 lv.2 프로그래머스 - level 2 실패 시도했지만 맞지 못한 문제

Comments

@suhyunsim
Copy link
Owner

문제

핵심 아이디어

  • DP로 풀이
  • 뒤에서부터 앞의 값을 통해 최대 값을 구하기
  • i + 1에서의 최댓값: i 최댓값 + (i + 1에서 선택할 수 있는 최댓값) => 반복해서 나올 수 있는 최종 값들 중 가장 큰 값을 return

어려운 점, 실수

  • 재귀로 풀면 시간초과

풀이

package main.java.com.poogle.PG.Q12913;

import java.util.Arrays;

public class Solution {
    public static void main(String[] args) {
        System.out.println(solution(new int[][]{{1, 2, 5, 5}, {5, 6, 8, 7}, {4, 3, 2, 1}}));
    }

    private static int solution(int[][] land) {
        for (int i = 1; i < land.length; i++) {
            land[i][0] += Math.max(Math.max(land[i - 1][1], land[i - 1][2]), land[i - 1][3]);
            land[i][1] += Math.max(Math.max(land[i - 1][0], land[i - 1][2]), land[i - 1][3]);
            land[i][2] += Math.max(Math.max(land[i - 1][1], land[i - 1][0]), land[i - 1][3]);
            land[i][3] += Math.max(Math.max(land[i - 1][1], land[i - 1][2]), land[i - 1][0]);
        }
        int [] answer = land[land.length - 1];
        Arrays.sort(answer);
        return answer[answer.length - 1];
    }
}
@suhyunsim suhyunsim self-assigned this Jul 7, 2021
@suhyunsim suhyunsim added DP 다이나믹 프로그래밍 lv.2 프로그래머스 - level 2 실패 시도했지만 맞지 못한 문제 labels Jul 7, 2021
@suhyunsim suhyunsim added this to the 7월 2주 차 milestone Jul 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DP 다이나믹 프로그래밍 lv.2 프로그래머스 - level 2 실패 시도했지만 맞지 못한 문제
Projects
None yet
Development

No branches or pull requests

1 participant