[Algorithm] programmers-12923-숫자 블둝

Β·

1 min read

import java.util.*;
/*
μ‹œκ°„μ΄ˆκ³Όκ°€ 자꾸 λ‚˜λŠ”κ±° λ³΄λ‹ˆ 이뢄탐색? -> μ•„λ‹ˆμ˜€λ‹€κ³  ν•œλ‹€.
*/
class Solution {    
    public int[] solution(long begin, long end) {
        int[] answer = new int[(int)(end-begin+1)];
        int idx = 0;

        for(long i = begin;i<=end;i++){
            answer[idx++] = find(i);
        }

        return answer;
    }

    static int find(long num){
        if(num == 1) return 0; //1의경우 0리턴
        int n = 2; //λ‚˜λˆŒ 수. 2λΆ€ν„° λ‚˜λˆˆλ‹€.
        int div = 1;
        while(n <= Math.sqrt(num)){
            if(num%n == 0) { //μ•½μˆ˜λΌλ©΄
                if(num/n<=10000000) return (int)num/n; //10λ§Œμ΄ν•˜μ˜ 수라면 리턴
                div = n;
            }
            n++;
        }
        return div;
    }
}
  • μ²˜μŒμ— 짰던 μ½”λ“œλŒ€λ‘œ ν•˜λ©΄ νš¨μœ¨μ„± ν…ŒμŠ€νŠΈμ—μ„œ μ‹œκ°„μ΄ˆκ³Όκ°€ 났닀.(근데 μ½”λ“œλ₯Ό μ§€μ›Œλ²„λ €μ„œ 기둝을 λͺ»λ‚¨κΉ€ γ…œγ…œ)

  • beginλΆ€ν„° endκΉŒμ§€ κ°€μž₯ μž‘μ€ μ•½μˆ˜λ‘œ λ‚˜λˆ„μ–΄μ§€λŠ” 수λ₯Ό μ°ΎλŠ”λ‹€.

Β