수열 2559

LJM·2023년 2월 23일
0

백준풀기

목록 보기
114/259

https://www.acmicpc.net/problem/2559

K의 숫자가 정해져 있어서 쉬운 문제였다..

시간복잡도 N-K+1 ?

import java.io.*;

public class Main
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] input = br.readLine().split(" ");
        int N = Integer.parseInt(input[0]);
        int K = Integer.parseInt(input[1]);

        int[] arr = new int[N];
        input = br.readLine().split(" ");
        for(int i = 0; i < N; ++i)
        {
            arr[i] = Integer.parseInt(input[i]);
        }

        int l = 0;
        int r = K-1;
        int sum = 0;
        for(int i = l; i <= r; ++i)
            sum += arr[i];
        int max = sum;

        while(true)
        {
            sum -= arr[l];
            l++;
            r++;
            if(r >= N)
                break;
            sum += arr[r];

            max = Math.max(max, sum);
        }

        System.out.println(max);
    }
}
profile
게임개발자 백엔드개발자

0개의 댓글