Skip to content

Commit

Permalink
Add #7 정수 삼각형 - 지선아
Browse files Browse the repository at this point in the history
  • Loading branch information
suna-ji authored Dec 9, 2024
1 parent 2c3af2f commit 40563a2
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions dp/정수 삼각형/지선아.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.*;

class Solution {
public int solution(int[][] triangle) {
int[][] dp = new int[triangle.length][triangle.length];
dp[0][0] = triangle[0][0];
for (int i = 1; i < triangle.length; i++) {
for (int j = 0; j < i + 1; j++) {
if (j == 0) { // 젤 왼쪽
dp[i][j] = triangle[i][j] + dp[i - 1][j];
} else if (j == i) { // 가장 오른쪽 열
dp[i][j] = triangle[i][j] + dp[i - 1][j - 1];
} else { // 중간 열
dp[i][j] = triangle[i][j] + Math.max(dp[i - 1][j], dp[i - 1][j - 1]);
}
}
}
return Arrays.stream(dp[triangle.length - 1]).max().getAsInt();
}
}

0 comments on commit 40563a2

Please sign in to comment.