본문 바로가기
알고리즘 & 자료구조/프로그래머스

최솟값 만들기

by 신재권 2022. 12. 27.
package programmers;

import java.util.Collections;
import java.util.PriorityQueue;

public class 최솟값만들기 {

   public static int solution(int[] A, int[] B) {
      int ans = 0;
      PriorityQueue<Integer> a = new PriorityQueue<>();
      PriorityQueue<Integer> b = new PriorityQueue<>(Collections.reverseOrder());

      for (int i = 0; i < A.length; i++) {
         a.add(A[i]);
         b.add(B[i]);
      }

      while (!a.isEmpty()) {
         ans += a.poll() * b.poll();
      }

      return ans;
   }

   public static void main(String[] args) {
      System.out.println(solution(new int[] {1, 4, 2}, new int[] {5, 4, 4}));
      System.out.println(solution(new int[] {1, 2}, new int[] {3, 4}));
   }
}

'알고리즘 & 자료구조 > 프로그래머스' 카테고리의 다른 글

이진 변환 반복하기  (0) 2022.12.29
올바른 괄호  (0) 2022.12.28
JadenCase 문자열 만들기  (0) 2022.12.26
최대값과 최솟값  (0) 2022.12.25
크기가 작은 부분 문자열  (0) 2022.12.24