BOJ_1806_부분합_java
2023. 1. 12. 17:15ㆍAlgorithm/BOJ
728x90
https://www.acmicpc.net/problem/1806
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
백준 1806번 부분합
골드 4
*/
public class BOJ_1806_부분합 {
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[] nums = new int[N];
st = new StringTokenizer(br.readLine());
for (int i = 0; i< N; i++){
nums[i] = Integer.parseInt(st.nextToken());
}
System.out.println(solution(nums, N, S));
}
public static int solution(int[] nums, int N, int S){
int left = 0;
int right = 0;
int sum = 0;
int min = Integer.MAX_VALUE;
while (right < N){
sum += nums[right];
if (sum >= S){
while (sum - nums[left] >= S && left <= right){
sum = sum - nums[left++];
}
min = Math.min(min, right - left + 1);
}
right++;
}
return min == Integer.MAX_VALUE ? 0 : min;
}
}
'Algorithm > BOJ' 카테고리의 다른 글
BOJ_17427_약수의 합 2_java (0) | 2023.01.14 |
---|---|
BOJ_9252_LCS 2_java (0) | 2023.01.13 |
BOJ_2467_용액_java (0) | 2023.01.12 |
BOJ_2166_다각형의면적_java (0) | 2023.01.11 |
BOJ_12852_1로 만들기 2_java (0) | 2023.01.10 |