Problem · Array
Select Object by Key Extreme
Learn this problemProblem statement
You are given n ordered string-to-integer objects. For the practice interface, each source object is represented by one unique entry in objectIds. For object i, the parallel arrays keys[i] and values[i] contain that object's keys and integer values. Returning an identifier represents returning its corresponding source object.
Select an object that contains targetKey:
- If
selectMaximumistrue, return the identifier of the eligible object with the largest value fortargetKey. - If
selectMaximumisfalse, return the identifier of the eligible object with the smallest value fortargetKey. - Ignore objects that do not contain
targetKey. - If several eligible objects have the same selected extreme value, return the earliest one in the original input order.
At least one object contains targetKey.
Function
selectObjectByKeyExtreme(objectIds: String[], keys: String[][], values: int[][], targetKey: String, selectMaximum: boolean) → StringExamples
Example 1
objectIds = ["alpha","beta","gamma"]keys = [["score","age"],["score"],["age","score"]]values = [[7,30],[3],[40,9]]targetKey = "score"selectMaximum = falsereturn = "beta"The eligible values for score are 7, 3, and 9. The minimum is 3, so the selected identifier is beta.
Example 2
objectIds = ["one","two","three"]keys = [["risk"],["risk"],["risk"]]values = [[8],[8],[5]]targetKey = "risk"selectMaximum = truereturn = "one"Objects one and two tie at the maximum value 8. The earlier object, one, is returned.
Example 3
objectIds = ["a","b","c"]keys = [["age"],["score","age"],["score"]]values = [[4],[10,5],[2]]targetKey = "score"selectMaximum = truereturn = "b"Object a is ignored because it has no score key. Between the remaining values 10 and 2, the maximum belongs to b.
Constraints
objectIds.length >= 1.keys.length == values.length == objectIds.length.- For every index
i,keys[i].length == values[i].length, and the keys withinkeys[i]are unique. - The identifiers in
objectIdsare unique. - At least one object contains
targetKey.