|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.BufferedWriter; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.io.OutputStreamWriter; |
| 7 | +import java.util.StringTokenizer; |
| 8 | + |
| 9 | +public class BJ_1806_부분합 { |
| 10 | + |
| 11 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 13 | + private static StringTokenizer st; |
| 14 | + |
| 15 | + private static int N; |
| 16 | + private static int S; |
| 17 | + private static int ans; |
| 18 | + private static int[] nums; |
| 19 | + |
| 20 | + public static void main(String[] args) throws IOException { |
| 21 | + init(); |
| 22 | + sol(); |
| 23 | + } |
| 24 | + |
| 25 | + private static void init() throws IOException { |
| 26 | + st = new StringTokenizer(br.readLine()); |
| 27 | + N = Integer.parseInt(st.nextToken()); |
| 28 | + S = Integer.parseInt(st.nextToken()); |
| 29 | + ans = Integer.MAX_VALUE; |
| 30 | + |
| 31 | + nums = new int[N]; |
| 32 | + st = new StringTokenizer(br.readLine()); |
| 33 | + for (int i = 0; i < N; i++) { |
| 34 | + nums[i] = Integer.parseInt(st.nextToken()); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private static void sol() throws IOException { |
| 39 | + int left = 0, right = 0; |
| 40 | + int tmp = 0; |
| 41 | + while (right < N) { |
| 42 | + tmp += nums[right]; |
| 43 | + |
| 44 | + while (tmp >= S) { |
| 45 | + ans = Math.min(ans, right - left + 1); |
| 46 | + tmp -= nums[left]; |
| 47 | + left++; |
| 48 | + } |
| 49 | + right++; |
| 50 | + } |
| 51 | + bw.write(ans != Integer.MAX_VALUE ? ans + "" : "0"); |
| 52 | + bw.flush(); |
| 53 | + bw.close(); |
| 54 | + br.close(); |
| 55 | + } |
| 56 | + |
| 57 | +} |
| 58 | +``` |
0 commit comments