Created
January 12, 2019 04:53
-
-
Save ClearNB/5ed89dae262886863012261c3e1e806a to your computer and use it in GitHub Desktop.
Maximum path sum I (Java 8 ver) Sample by ClearNB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Solution { | |
public static void main(String[] args) { | |
try (Scanner sc = new Scanner(System.in)) { | |
int T = sc.nextInt(); | |
for(int a0 = 0; a0 < T; a0++) { | |
int N = sc.nextInt(); | |
int[][] NumData = new int[N][N]; | |
for(int i = 0; i < N; i++) { | |
String line = sc.next(); | |
line += sc.nextLine(); | |
String[] lines = line.split(" "); | |
for(int j = 0; j < lines.length; j++) { | |
NumData[i][j] = Integer.parseInt(lines[j]); | |
} | |
} | |
for (int i = N - 2; i >= 0; i--) { | |
for (int j = 0; j <= i; j++) { | |
NumData[i][j] = Math.max(NumData[i + 1][j], NumData[i + 1][j + 1]) + NumData[i][j]; | |
} | |
} | |
System.out.println(NumData[0][0]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment