SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
n*2차원배열을 만들어서 더한값과 곱한값을 저장후 큰값을 저장한다.
import java.util.Scanner;
class Solution{
public static void main(String args[]) throws Exception{
Scanner sc = new Scanner(System.in);
int T;
T=sc.nextInt();
for(int test_case = 1; test_case <= T; test_case++) {
int n=sc.nextInt();
int arr[][]=new int[n][2];
for(int i=0;i<n;i++){
int input=sc.nextInt();
if(i==0){
arr[i][0]=input;
arr[i][1]=input;
}else {
arr[i][0]=Math.max(arr[i-1][0],arr[i-1][1])*input;
arr[i][1]=Math.max(arr[i-1][0],arr[i-1][1])+input;
}
}
System.out.println("#"+test_case+" "+Math.max(arr[n-1][0],arr[n-1][1]));
}
}
}
'알고리즘 > 삼성 SW expert Academy' 카테고리의 다른 글
[SWEA] 7985. Rooted Binary Tree 재구성_JAVA (0) | 2022.05.26 |
---|---|
[SWEA] 8104. 조 만들기_JAVA (0) | 2022.05.26 |
[SWEA] 8457. 알 덴테 스파게티_JAVA (0) | 2022.05.25 |
[SWEA] 8673. 코딩 토너먼트1_JAVA (0) | 2022.05.24 |
[SWEA] 8658. Summation_JAVA (0) | 2022.05.24 |