1. 문제 링크
2559번: 수열
첫째 줄에는 두 개의 정수 N과 K가 한 개의 공백을 사이에 두고 순서대로 주어진다. 첫 번째 정수 N은 온도를 측정한 전체 날짜의 수이다. N은 2 이상 100,000 이하이다. 두 번째 정수 K는 합을 구하기
www.acmicpc.net
2. 문제 및 입출력예제
3. 문제 풀이
입력받을 때 누적합을 배열에 넣었다.
주어진 값만큼 차이나게 값을 빼면서 그 중 최대가 되는 값을 출력했다.
4. 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
String[] split = br.readLine().split(" ");
int N = Integer.parseInt(split[0]); //전체 날짜 수
int K = Integer.parseInt(split[1]); //연속적인 날짜 수
int[] temp = new int[N+1];
split = br.readLine().split(" ");
for(int i=1;i<N+1;i++) {
temp[i] = temp[i-1] + Integer.parseInt(split[i-1]);
}
int max = Integer.MIN_VALUE;
for(int i=0;i<N-K+1;i++) {
int minus = temp[i+K] - temp[i];
max = Math.max(max, minus);
}
System.out.println(max);
}
}
'ALGORITHM' 카테고리의 다른 글
백준 2606 바이러스 (Java) (1) | 2023.03.15 |
---|---|
백준 14889 스타트와 링크 (Java) (0) | 2023.03.14 |
백준 4659 비밀번호 발음하기 (Java) (0) | 2023.03.13 |
백준 15686 치킨 배달 (Java) (0) | 2023.03.13 |
백준 9372 상근이의 여행 (Java) (0) | 2023.03.12 |