Uncategorized

[알고리즘] 나누어 떨어지는 숫자 배열

import java.util.*;

class Solution {
    public int[] solution(int[] arr, int divisor) {
        int[] answer = Arrays.stream(arr).filter(i->i%divisor==0).sorted().toArray();
        return answer.length != 0 ? answer : new int[]{-1};
    }
}
import java.util.*;

class Solution {
    public int[] solution(int[] arr, int divisor) {
        PriorityQueue<Integer> pq = new PriorityQueue<>((next,prev)->{
            if(prev.intValue()>next.intValue()) return -1;
            return 1;
        });
        
        for(int v : arr) {
            if(v%divisor == 0) pq.add(v);
        }
        
        int[] answer = new int[pq.size()];
        for(int i=0;i<answer.length;i++) { //여기에 pq.size쓰면 안된다. pq가 작아지므로....... 
            answer[i] = pq.poll();
        }
        return answer.length!=0 ? answer : new int[]{-1};
    }
    
    
}
import java.util.*;
import java.util.stream.*;

class Solution {
    public int[] solution(int[] arr, int divisor) {
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        
        for(int v : arr) {
            if(v%divisor == 0) pq.add(v);
        }        
        
        int[] answer = Stream.generate(pq::poll).limit(pq.size()).mapToInt(a->a).toArray();
        return answer.length!=0 ? answer : new int[]{-1};
    }
    
    
}
https://github.com/imkim1893/algorithms/blob/master/%EB%82%98%EB%88%84%EC%96%B4%EB%96%A8%EC%96%B4%EC%A7%80%EB%8A%94%EC%88%AB%EC%9E%90%EB%B0%B0%EC%97%B4.java