Skip to content

Commit

Permalink
부분합
Browse files Browse the repository at this point in the history
  • Loading branch information
mymycse committed Sep 4, 2024
1 parent 4996725 commit 4df7bdd
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions 조민영/1806 부분합.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.io.*;
import java.util.*;

public class g1806 {
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 start = 0;
int end = 0;
int sum = 0;
int min = Integer.MAX_VALUE;
boolean exist = false;

while ( true ) {
if ( sum >= s ) {
min = Math.min(min, end-start);
sum -= arr[start++];
exist = true;
} else if ( end == n )
break;
else
sum += arr[end++];
}

System.out.println(exist ? min : 0 );
}
}

0 comments on commit 4df7bdd

Please sign in to comment.