Rank Linked Merchants by Weighted Shared Attributes
Learn this problemProblem statement
You are given merchant records, attribute weights, and a target merchant ID.
Each row in merchants starts with a unique merchant ID and then contains alternating attribute names and values: [merchantId, name1, value1, name2, value2, ...]. Attribute names are unique within one row.
Each entry in attributeWeights has the form attributeName|weight. An attribute contributes its weight to another merchant's link score when that merchant and the target have exactly the same attribute name and value. Attributes without a supplied weight contribute zero.
Exclude the target. A different merchant is linked when its score is positive. Return every linked merchant as merchantId|score, ordered by descending score and then by ascending merchant ID.
When every supplied weight is 1, the result contains exactly the merchants that share at least one weighted attribute with the target.
Function
rankLinkedMerchants(merchants: String[][], attributeWeights: String[], targetMerchantId: String) → String[]Examples
Example 1
merchants = [["m1","email","a@x","country","US","phone","111"],["m2","email","a@x","country","US","phone","999"],["m3","email","z@x","country","US","phone","111"],["m4","email","a@x","country","CA","phone","111"],["m5","email","z@x","country","CA"]]attributeWeights = ["email|5","phone|3","country|2"]targetMerchantId = "m1"return = ["m4|8","m2|7","m3|5"]m4 matches email and phone for score 8; m2 matches email and country for 7; m3 matches phone and country for 5. m5 scores zero.
Example 2
merchants = [["target","email","root@x","note","same"],["b","email","root@x"],["a","email","root@x"],["c","note","same"]]attributeWeights = ["email|4","note|0"]targetMerchantId = "target"return = ["a|4","b|4"]a and b tie and are ordered by ID. Although c matches note, that attribute has weight zero, so c is not linked.
Example 3
merchants = [["solo","region","west"]]attributeWeights = ["region|10"]targetMerchantId = "solo"return = []The target is excluded and there are no other merchants.
Constraints
1 <= merchants.length <= 100000.- Every merchant row contains a unique nonempty ID followed by at most
20attribute-name/value pairs. - Merchant IDs, attribute names, and attribute values are printable ASCII strings of length at most
100and contain no pipe character. 1 <= attributeWeights.length <= 20, and every weighted attribute name appears at most once.- Every weight is an integer from
0through1000000. targetMerchantIdidentifies exactly one merchant.