[백준/2559] 꿀 아르바이트 - JAVA

이지환·2023년 12월 20일

알고리즘(백준) 💻

목록 보기
21/80
post-thumbnail

📌 문제

알고리즘 분류 : 투포인터, 누적합
난이도 : 실버3
출처 : 백준 - 꿀 아르바이트

🦧 문제 풀이 접근

누적합으로 배열에 값을 넣는다.
누적합을 이용해 M개의 합의 최대값을 구한다.

💻 code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine()," ");
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        long[] arr = new long[N+1];
        long max = 0;
        st = new StringTokenizer(br.readLine()," ");
        for(int i=1;i<=N;i++) {
            arr[i] = arr[i-1]+Integer.parseInt(st.nextToken());
        }
        for(int i=0;i<N-M+1;i++) {
            max = Math.max(max, arr[i+M]-arr[i]);
        }
        System.out.println(max);
    }
}

🥇 결과

🎓 느낀점

어려운 문제는 아니었으나 배열을 int가 아닌 long으로 해야 하는점, 0일 근무도 가능하다는 점을 잘 고려해야 한다.

profile
takeitEasy

0개의 댓글