문제링크 : https://programmers.co.kr/learn/courses/30/lessons/43105
class Solution {
public static int dp[][];
public static int triangles[][];
public static int dfs(int y,int x){
if(y==triangles.length-1) return dp[y][x] = triangles[y][x];
if(dp[y][x] != -1) return dp[y][x];
return dp[y][x] = Math.max(dfs(y+1,x),dfs(y+1,x+1)) + triangles[y][x];
}
public static int solution(int[][] triangle) {
int answer = 0;
int N = triangle.length;
dp = new int[N][N];
triangles = new int[N][N];
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
dp[i][j] = -1;
}
}
for(int i=0;i<N;i++){
for(int j=0;j<i+1;j++){
triangles[i][j] = triangle[i][j];
}
}
answer = dfs(0,0);
return answer;
}
}
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 순위 / JAVA (0) | 2022.02.18 |
---|---|
[프로그래머스] 네트워크 / JAVA (0) | 2022.02.18 |
[프로그래머스] 디스크 컨트롤러 / JAVA (0) | 2022.02.18 |
[프로그래머스] 입국심사 / JAVA (0) | 2022.02.18 |
[프로그래머스] 2021 카카오인턴십 - 미로탈출 [JAVA] (0) | 2021.08.31 |