Database Rows — Minimum by Key
Problem statement
A table contains ordered rows of string-keyed integer values. Each row is represented by its original ordered array of entries such as "a=-3". An entry has the form key=value; keys within one row are unique.
Implement minByKey(records, key). Return the row with the smallest numeric value at the supplied key.
- An absent comparison key counts as zero, including in an empty row.
- If all requested values tie, choose the first input row.
- Return exactly the selected row's original strings in their original order. Keep unrelated entries and absent keys as they were; a selected empty row returns
[].
Function
minByKey(records: String[][], key: String) → String[]Examples
Example 1
records = [["a=1"],["a=2"],["a=3"]]key = "a"return = ["a=1"]The a values are 1, 2 and 3, so the first row is the minimum. Values match the complete source example; key=value is the practice representation.
Example 2
records = [["a=1","b=2"],["a=3","b=0"]]key = "b"return = ["a=3","b=0"]The second row has b=0, below b=2. Return both original entries in their original order.
Example 3
records = [["a=1","b=2"],["a=3"]]key = "b"return = ["a=3"]The second row lacks b, so its comparison value is zero. Return ["a=3"] without adding b.
Constraints
1 <= records.length <= 20.- Each row contains at most 12 entries; the entire table contains at most 32 entries.
- Every key matches
[a-z][a-z0-9_]{0,11}and keys are unique within each row. Keys are case-sensitive. - Each entry is exactly
key=value, with no whitespace. A value is0or a nonzero canonical signed decimal: no plus sign, leading zeros or negative zero. - Integer values lie between
-10^6and10^6, inclusive. - The supplied key follows the same key grammar and may be absent from every row.