आसान 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 के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।