ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 1806 부분합
    백준/두 포인터 2024. 8. 2. 23:28

     

     

     

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.StringTokenizer;
    
    public class Main {
    	
        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 sum[] = new int[N+1];
           	  st = new StringTokenizer(br.readLine(), " ");
           	  sum[0] = 0;
           	  
           	  for(int i=1; i<=N; i++) {
           		  sum[i] = Integer.parseInt(st.nextToken()) + sum[i-1];
           	  }      	 
           	 
           	  int left = 0;
           	  int right = 1;
           	  
           	  int value = 0;
           	  int result = Integer.MAX_VALUE;
           	  
           	  while(left<right && right<=N) {
           		  
           		  value = sum[right] - sum[left];
           	
           		  if(value>=S) {
           			  
           			  left++;  			 
           			  result = Math.min(result, right-left + 1);
           				    
           		  }else {
           			  right++;
           		  } 		  
           		  
           	  }
           	  
           	  // 합을 만드는 것이 불가능하다면 0을 출력
           	    if(result== Integer.MAX_VALUE) {
           		  
           		     result = 0;
           	     }
           	    
           	  System.out.println(result);     	  
           	  
        }
    }

     

    '백준 > 두 포인터' 카테고리의 다른 글

    3151 합이 0  (0) 2024.08.02
    1484 다이어트  (0) 2022.06.11
    2461 대표 선수  (0) 2022.05.15
    1253 좋다  (0) 2022.04.18

    댓글

Designed by Tistory.