forked from wotjd4305/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproDP2.java
More file actions
73 lines (52 loc) · 1.65 KB
/
proDP2.java
File metadata and controls
73 lines (52 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public class proDP2 {
public static void main(String[] args) {
int[][] triangle = {{7},{3,8},{8,1,0},{2,7,4,4},{4,5,2,6,5}};
System.out.println(solution(triangle));
//showArray(triangle);
}
public static int solution(int[][] triangle) {
int answer = 0;
int idx = 1;
//1)겉에꺼 처리
//i=0일때 길이1, i=1일때 길이2
for(int i =1; i<triangle.length; i++)
{
int ex_end_idx = triangle[i-1].length-1;
int after_end_idx = triangle[i].length-1;
//
triangle[i][0] += triangle[i-1][0];
triangle[i][after_end_idx] += triangle[i-1][ex_end_idx];
}
//2)속에꺼 처리리
//규칙
//i는 2부터 시작하면 됨.
// 2,1은 1,0과 1,1 둘중하나 -> 식으로가능
// 4,1은 3,0과 3,1 둘중하나
for(int i =2; i<triangle.length; i++)
for(int j =1; j<triangle[i].length-1; j++)
{
triangle[i][j] += Math.max(triangle[i-1][j-1], triangle[i-1][j]);
}
answer = findMaxValue(triangle);
return answer;
}
public static int findMaxValue(int[][] Array)
{
int max =0;
//맨 밑에 있는 애들
for(int i=0; i<Array[Array.length-1].length; i++)
{
max = Math.max(Array[Array.length-1][i], max);
}
return max;
}
public static void showArray(int[][] Array)
{
for(int[] i : Array) {
for (int j : i) {
System.out.print(j + " ");
}
System.out.println();
}
}
}