모든 챌린지
쉬움 0명 해결 정답률 0%

Join Customers and Orders

Join customers with their orders like a LINQ Join and report each customer's total spend.

배우게 될 내용

  • Match two collections on a shared key
  • Sum values per group
  • Sort by a value and break ties by name

문제

번역본 보기

LINQ's Join matches items from two sequences by a shared key, much like a database join. Here, customers are matched with their orders by customer id.

Customer i has id customer_ids[i] and name customer_names[i]. Each order is [customer_id, amount].

Join the two: for every customer with at least one order, add up the amounts of their orders. Customers without orders are left out, and orders whose customer id is unknown are ignored.

Implement join_customers_orders(customer_ids, customer_names, orders).

Input

customer_ids, customer_names: The customers (ids are distinct).

orders: [customer_id, amount] pairs.

Output

Return "<name>:<total>" for each customer with orders, sorted by total from highest to lowest, and by name (ordinal order) when totals are equal.

제약 조건

1 ≤ number of customers ≤ 10,000

0 ≤ number of orders ≤ 100,000; 1 ≤ amount ≤ 10^6

예시

예시 1
입력:
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 has two orders (50 + 20 = 70) and Lin one (30); Bo has none and is left out: ["Ada:70", "Lin:30"].

예시 2
입력:
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"};

설명

Both total 40, so they are ordered by name; the order for unknown id 9 is ignored: ["Max:40", "Zoe:40"].

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

First add up the order amounts per customer id.

힌트 2

Then go through the customers and keep those that have a total.

힌트 3

Sort by total descending, then by name in ordinal order.

예상 복잡도

O(n log n) time, O(n) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.