01-09 18:44
Recent Posts
Recent Comments
관리 메뉴

miinsun

[Algorithm]μ•Œκ³ λ¦¬μ¦˜ μžλ°”_28 μ—°μ†λœ μžμ—°μˆ˜μ˜ ν•© λ³Έλ¬Έ

Algorithm/Java

[Algorithm]μ•Œκ³ λ¦¬μ¦˜ μžλ°”_28 μ—°μ†λœ μžμ—°μˆ˜μ˜ ν•©

miinsun 2022. 1. 4. 13:02

 

πŸ’¬ λ¬Έμ œ μ„€λͺ…

Nμž…λ ₯으둜 μ–‘μ˜ μ •μˆ˜ N이 μž…λ ₯되면 2개 μ΄μƒμ˜ μ—°μ†λœ μžμ—°μˆ˜μ˜ ν•©μœΌλ‘œ μ •μˆ˜ N을 ν‘œν˜„ν•˜λŠ” λ°©λ²•μ˜ κ°€μ§“μˆ˜λ₯Ό 좜λ ₯ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ„Έμš”.

λ§Œμ•½ N=15이면
7+8=15
4+5+6=15
1+2+3+4+5=15
와 같이 총 3κ°€μ§€μ˜ κ²½μš°κ°€ μ‘΄μž¬ν•œλ‹€.

 

 

 

πŸ”¨ μž…μΆœλ ₯ 예

μž…λ ₯ - 첫 번째 쀄에 μ–‘μ˜ μ •μˆ˜ N(7<=N<1000)이 μ£Όμ–΄μ§‘λ‹ˆλ‹€.

15

좜λ ₯ - 첫 쀄에 총 경우수λ₯Ό 좜λ ₯ν•©λ‹ˆλ‹€.

3

 

 

​

πŸ’» Solution.java

import java.util.Scanner;
  
public class Main {
	public int solution(int n) {
		int answer = 0, sum = 0, lt = 0;
        int m = n / 2 + 1;
        int[] arr = new int[m];
        
        //μ΄ˆκΈ°ν™”
        for(int i = 0; i < m; i++) arr[i] = i + 1;
        
        //sliding window
        for(int rt = 0; rt < m; rt++){
        	sum += arr[rt];
            if(sum == n) answer++;
            while(sum >= n){
            	sum -= arr[lt++];
                if(sum == n) answer++;
            }
        }
        return answer;
	}
	
	public static void main(String[] args){
		Main main  = new Main();
		Scanner sc =new Scanner(System.in);
        int n = sc.nextInt();
		System.out.println(main.solution(n));
		sc.close();
	}
}

 

 

 

Comments