1. ๋ฌธ์ œ : https://www.acmicpc.net/problem/1806

 

2. ํ’€์ด

ํˆฌํฌ์ธํ„ฐ ์ด์šฉํ•ด์„œ ์ตœ์†Œ๊ฐ’ ์ฐพ๋Š” ๋ฌธ์ œ

total์ด s๋ฅผ ๋„˜์–ด๊ฐ€๋Š” ์ˆœ๊ฐ„์„ ์žก์•„์„œ min๊ฐ’์„ ๊ณ„์† ์—…๋ฐ์ดํŠธ ํ•ด์คŒ

 

3. ์ฝ”๋“œ

import java.util.Scanner;

public class ๋ถ€๋ถ„ํ•ฉ {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int n = scan.nextInt();
        int s = scan.nextInt();

        int[] nums = new int[n + 1];
        for (int i = 0; i < n; i++) {
            nums[i] = scan.nextInt();
        }

        int min = Integer.MAX_VALUE;
        int start = 0;
        int end = 0;
        int total = 0;
        while (start <= n && end <= n) {
            if (total >= s && min > end - start) min = end - start;

            if (total < s) total += nums[end++];
            else total -= nums[start++];
        }

        if (min == Integer.MAX_VALUE) System.out.println("0");
        else System.out.println(min);
    }
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ