問題
LINQ の Join は、2 つのシーケンスの要素を共通のキーで結び付けます。データベースの結合と似ています。ここでは顧客 ID で顧客と注文を結び付けます。
顧客 i の ID は customer_ids[i]、名前は customer_names[i] です。各注文は [customer_id, amount] です。
2 つを結合し、注文が 1 件以上ある顧客ごとに注文金額を合計します。注文のない顧客は除き、存在しない顧客 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 の注文は 2 件(50 + 20 = 70)、Lin は 1 件(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 つずつ開いてみましょう。
ヒント 1
まず顧客 ID ごとに注文金額を合計します。
ヒント 2
次に顧客を順に見て、合計がある人だけを残します。
ヒント 3
合計の大きい順、次に名前の文字コード順に並べます。
目安の計算量
時間 O(n log n)、追加メモリ O(n)
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。