프로그래머스 자바 숫자의표현

yjkim·2023년 8월 2일
0

알고리즘

목록 보기
36/59

문제 : https://school.programmers.co.kr/learn/courses/30/lessons/12924

접근

처음에는 bruteforce 로 접근하려고 하였으나 n이 최댓값이 10000이어서 이 방법은 pass,
dp, 약수 갯수 등등 다른 여러 방법등을 생각하고 이중포인터 방법이 적합할 것이라고 생각되어 채택

class Solution {
    public int solution(int n) {
        if (n==1 || n==2){
            return 1;
        }
        int total=1;
        int start=1;
        int end=1;
        int count=0;
        while(start<=end){
            if (total<n){
                end++;
                total+=end;
            }
            else if (total==n){
                count++;
                total-=start;
                start++;
            }
            else{
                total-=start;
                start++;
            }
            
        }
        return count;    
    }
}

추가

이중포인터나 이진탐색 알고리즘을 사용할 때 while 루프의 탈출 조건과 start, end값의 초기 설정에서 헷갈리지 않도록 주의

profile
컴퓨터 공부 / 백엔드 개발

0개의 댓글