Coding - Algo/Java

[SWEA] 3819:최대 부분 배열(Java 자바)

jainn 2022. 3. 17. 23:27
728x90

풀이 및 소스코드

import java.io.BufferedReader;
import java.io.InputStreamReader;

class Solution {
	public static void main(String args[]) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();

		int T = Integer.parseInt(br.readLine());

		for (int test_case = 1; test_case <= T; test_case++) {
			int res = -Integer.MAX_VALUE;
			
			int n = Integer.parseInt(br.readLine());
			
			int[] dp = new int[n+1];
			for(int i=1;i<n+1;i++) {
				int a = Integer.parseInt(br.readLine());
				dp[i] = Math.max(a, dp[i-1]+a);
				res = Math.max(dp[i], res);
			}
			
			sb.append("#").append(test_case).append(" ").append(res).append("\n");
		}
		sb.setLength(sb.length()-1);
		System.out.println(sb);
	}
}
반응형