본문 바로가기

CodingTEST

[SW Expert D2] 1959. 두 개의 숫자열

반응형

1959. 두 개의 숫자열

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

문제 분석

 

  • 두 배열을 마주보는 연속된 수를 곱해서 더했을 때 가장 큰 수를 출력해라

 

해결 포인트

 

  • 더 길이가 긴 배열을 찾는다. 
  • 작은 배열 시작 점이 0부터 끝 점이 긴 배열의 N까지 마주보는 수는 곱한다.
  • 배열 복사 : Arrays.copyOfRange(maxValue, i, i + minValue.length);
 

 

코드

 

import java.util.Arrays;
import java.util.Scanner;
import java.io.FileInputStream;

class Solution
{
    public static void main(String args[]) throws Exception
    {
        //System.setIn(new FileInputStream("res/input.txt"));

        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 M = sc.nextInt();

            int A [] =new int[N];
            for (int i = 0; i < N; i++) {
                A[i] = sc.nextInt();
            }

            int B [] =new int[M];
            for (int i = 0; i < M; i++) {
                B[i] = sc.nextInt();
            }

            int[] maxValue, minValue;

            if(N >= M) {
                maxValue = A;
                minValue = B;
            } else {
                maxValue = B;
                minValue = A;
            }

            int max = 0;
            for (int i = 0; i <= maxValue.length - minValue.length; i++) {
                int[] value = Arrays.copyOfRange(maxValue, i, i + minValue.length);
                for (int j = 0; j < minValue.length; j++) {
                    value[j] *= minValue[j];
                }
                max = Math.max(max, Arrays.stream(value).sum());
            }
            System.out.printf("#%d %d\n", test_case, max);
        }
    }
}
반응형