ColorApproximation
TCCC07 Qual 3 · 2007-07-30 · by ivan_metelsky
Problem Statement
In the RGB color model, colors are encoded as triples (R, G, B), where R, G and B denote the quantity of red, green and blue, respectively, in the color. Each number in the triple is between 0 and 255, inclusive. The distance between two colors (R1, G1, B1) and (R2, G2, B2) is max{|R1-R2|, |G1-G2|, |B1-B2|}. The distance between a color C and a set of colors S is the maximum among the distances between C and each color in S. A best approximation for a set of colors S is a color C such that the distance between C and S is as small as possible.
You will be given a
Notes
- Digits come before letters in the alphabetical order. For example, String "A9" is alphabetically less than String "AA".
Constraints
- colors will contain between 1 and 50 elements, inclusive.
- Each element of colors will contain between 6 and 48 characters, inclusive.
- Each element of colors will contain a single space separated list of colors without leading or trailing spaces.
- Each color in each element of colors will contain exactly 6 hexadecimal digits ('0'-'9', 'A'-'F').
- All colors will be distinct.
{"F36F4E"}
Returns: "F36F4E"
If a set contains only one color, that color is the set's best approximation.
{"7A946B","3B6473"}
Returns: "5A7453"
This set contains two colors (122, 148, 107) and (59, 100, 115). Every best approximation for this set will have a distance of 32 from it. The one among them whose hexadecimal representation comes first alphabetically is (90, 116, 83).
{"40FE20", "800040"}
Returns: "017F00"
{"626848 321D3A 60EACA 521A88 9C188F",
"94F0B3 B55795 B34004 20983C 7A644B"}
Returns: "49845E"
{"000000"}
Returns: "000000"
Submissions are judged against all 66 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class ColorApproximation with a public method string bestApproximation(vector<string> colors) · 66 test cases · 2 s / 256 MB per case