with production as(
select a.product_id, b.units, a.price, (b.units * a.price) as summary
from Prices a
left join UnitsSold b
on a.product_id = b.product_id
where b.purchase_date between a.start_date and a.end_date
)
select a.product_id, ifnull(round(sum(b.summary)/sum(b.units), 2), 0) as average_price
from Prices a
left join production b
on a.product_id = b.product_id
group by a.product_id
;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
class Solution {
public String solution(String X, String Y) {
String answer = "";
char[] xs = X.toCharArray();
char[] ys = Y.toCharArray();
List<Character> list = new ArrayList<>();
for(char xi : xs){
for (int i = 0; i < ys.length; i++) {
if (ys[i] == ' ') {
continue;
}
if (ys[i] == xi) {
list.add(xi);
ys[i] = ' ';
break;
}
}
}
list.sort(Comparator.reverseOrder());
if(list.isEmpty()){
return "-1";
} else if (list.get(0) == '0') {
return "0";
} else {
for (char c : list){
answer += c;
}
return answer;
}
}
}
런타임 에러발생으로 아래처럼 수정
import java.util.*;
class Solution {
public String solution(String X, String Y) {
StringBuilder answerBuilder = new StringBuilder();
char[] xs = X.toCharArray();
char[] ys = Y.toCharArray();
Arrays.sort(xs);
Arrays.sort(ys);
// 두 배열의 끝에서부터 비교
int xPtr = xs.length - 1;
int yPtr = ys.length - 1;
while (xPtr >= 0 && yPtr >= 0) {
if (xs[xPtr] > ys[yPtr]) {
xPtr--; // X의 숫자가 더 크면 X 포인터만 이동
} else if (xs[xPtr] < ys[yPtr]) {
yPtr--; // Y의 숫자가 더 크면 Y 포인터만 이동
} else { // 두 숫자가 같으면 짝꿍이므로 결과에 추가
answerBuilder.append(xs[xPtr]);
xPtr--;
yPtr--;
}
}
String answer = answerBuilder.toString();
if (answer.isEmpty()) {
return "-1";
} else if (answer.startsWith("0")) {
return "0";
} else {
return answer;
}
}
}