240621 부분합

Jongleee·2024년 6월 21일
0

TIL

목록 보기
605/737
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 s = Integer.parseInt(st.nextToken());
	int[] arr = new int[n];

	st = new StringTokenizer(br.readLine());
	for (int i = 0; i < n; i++) {
		arr[i] = Integer.parseInt(st.nextToken());
	}

	int count = 1;
	int sum = 0;
	int rightIdx = 0;
	int minLength = Integer.MAX_VALUE;

	for (int i = 0; i < n; i++) {
		count--;
		while (sum < s && rightIdx < n) {
			sum += arr[rightIdx++];
			count++;
		}
		if (sum >= s) {
			minLength = Math.min(minLength, count);
			sum -= arr[i];
		}
	}

	if (minLength == Integer.MAX_VALUE) {
		System.out.println("0");
	} else {
		System.out.println(minLength);
	}
}

출처:https://www.acmicpc.net/problem/1806

0개의 댓글