💻 문제 출처 : 프로그래머스_최솟값 만들기
import java.util.*;
class Solution
{
public int solution(int []A, int []B) {
int answer = 0;
int arrayLength = A.length;
Arrays.sort(A);
Arrays.sort(B);
for(int i = 0; i < arrayLength; i++) {
answer += A[i] * B[arrayLength - 1 - i];
}
return answer;
}
}
📌 문제 풀이 설명
import java.util.*;
class Solution {
public int solution(int []A, int []B) {
int answer = 0;
int length = A.length;
quickSort(A, 0, A.length-1);
quickSort(B, 0, B.length-1);
for(int i = 0 ; i < length ; i++)
{
answer += A[i] * B[length-1-i];
}
return answer;
}
public static void quickSort(int[] arr, int left, int right) {
int i, j, pivot, tmp;
if (left < right) {
i = left;
j = right;
pivot = arr[left];
//분할 과정
while (i < j) {
while (arr[j] > pivot) j--;
while (i < j && arr[i] <= pivot) i++;
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
arr[left] = arr[i];
arr[i] = pivot;
//정렬 과정
quickSort(arr, left, i - 1);
quickSort(arr, i + 1, right);
}
}
}
📌 문제 풀이 설명