투포인터 알고리즘

채종윤·2023년 7월 13일

투포인터 알고리즘이란?

📔 문제 설명
https://www.acmicpc.net/problem/2018


💡 내 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());

	int sum = 1;
	int count =1; // N이 15일때 숫자 15만 뽑는 경우의 수 
	int start_index=1;
	int end_index=1;
	
	while(end_index!=N) {
		
		if(sum<N) {
			end_index++;
			sum = sum+end_index;
			
		}
		else if(sum>N) {
			sum = sum - start_index;
			start_index++;
			
		}
		else if(sum ==N) {
			count++;
			end_index++;
			
			sum = sum+end_index;
			
		}
	}
	System.out.println(count);
}

}

✍느낀점
//시간복잡도 :O(n), 최댓값 10,000,000이므로 O(nlogn)의 알고리즘을 사용하면 시간초과

profile
안녕하세요. 백앤드 개발자를 목표로 하고 있습니다!

0개의 댓글