문제
LINQ의 Join은 두 시퀀스의 항목을 공통 키로 연결합니다. 데이터베이스 조인과 비슷합니다. 여기서는 고객 ID로 고객과 주문을 연결합니다.
고객 i의 ID는 customer_ids[i], 이름은 customer_names[i]입니다. 각 주문은 [customer_id, amount]입니다.
두 시퀀스를 조인해 주문이 하나 이상 있는 고객마다 주문 금액을 합하세요. 주문이 없는 고객은 빼고, 알 수 없는 고객 ID의 주문은 무시합니다.
join_customers_orders(customer_ids, customer_names, orders)를 구현하세요.
입력
customer_ids, customer_names: 고객 목록(ID는 서로 다름).
orders: [customer_id, amount] 쌍.
출력
주문이 있는 고객마다 "<name>:<total>"을 합계가 큰 순서로, 합계가 같으면 이름순(문자 코드 순)으로 반환합니다.
제약 조건
1 ≤ 고객 수 ≤ 10,000
0 ≤ 주문 수 ≤ 100,000; 1 ≤ amount ≤ 10^6
예시
입력:
orders = [
[1, 50],
[2, 30],
[1, 20]
]
customer_ids = [1, 2, 3]
customer_names = ["Ada", "Lin", "Bo"]
예상 출력:
expected = ["Ada:70", "Lin:30"]
입력:
std::vector<std::vector<long long>> orders = {
{1, 50},
{2, 30},
{1, 20}
};
std::vector<long long> customer_ids = {1, 2, 3};
std::vector<std::string> customer_names = {"Ada", "Lin", "Bo"};
예상 출력:
std::vector<std::string> expected = {"Ada:70", "Lin:30"};
입력:
long[][] orders = new long[][] {
new long[] {1, 50},
new long[] {2, 30},
new long[] {1, 20}
};
long[] customer_ids = new long[] {1, 2, 3};
string[] customer_names = new string[] {"Ada", "Lin", "Bo"};
예상 출력:
string[] expected = new string[] {"Ada:70", "Lin:30"};
설명
Ada는 주문이 두 건(50 + 20 = 70), Lin은 한 건(30)입니다. Bo는 주문이 없어 빠집니다: ["Ada:70", "Lin:30"].
입력:
orders = [
[8, 40],
[7, 40],
[9, 99]
]
customer_ids = [7, 8]
customer_names = ["Zoe", "Max"]
예상 출력:
expected = ["Max:40", "Zoe:40"]
입력:
std::vector<std::vector<long long>> orders = {
{8, 40},
{7, 40},
{9, 99}
};
std::vector<long long> customer_ids = {7, 8};
std::vector<std::string> customer_names = {"Zoe", "Max"};
예상 출력:
std::vector<std::string> expected = {"Max:40", "Zoe:40"};
입력:
long[][] orders = new long[][] {
new long[] {8, 40},
new long[] {7, 40},
new long[] {9, 99}
};
long[] customer_ids = new long[] {7, 8};
string[] customer_names = new string[] {"Zoe", "Max"};
예상 출력:
string[] expected = new string[] {"Max:40", "Zoe:40"};
설명
둘 다 합계가 40이라 이름순으로 정렬합니다. 알 수 없는 ID 9의 주문은 무시합니다: ["Max:40", "Zoe:40"].
접근 방법
먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.
힌트 1
먼저 고객 ID별로 주문 금액을 합하세요.
힌트 2
그다음 고객을 훑으며 합계가 있는 사람만 남기세요.
힌트 3
합계 내림차순, 그다음 이름의 문자 코드 순으로 정렬하세요.
예상 복잡도
시간 O(n log n), 추가 공간 O(n)
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.