[LeetCode] Similar RGB Color

800. Similar RGB Color

The red-green-blue color “#AABBCC” can be written as “#ABC” in shorthand.

  • For example, “#15c” is shorthand for the color “#1155cc”.

The similarity between the two colors “#ABCDEF” and “#UVWXYZ” is -(AB - UV)2 - (CD - WX)2 - (EF - YZ)2.

Given a string color that follows the format “#ABCDEF”, return a string represents the color that is most similar to the given color and has a shorthand (i.e., it can be represented as some “#XYZ”).

Any answer which has the same highest similarity as the best answer will be accepted.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
long long b10(string s) {
long long res = 0;
for(int i = 0; i < 2; i++) {
long long now = 0;
if(isdigit(s[i])) now = s[i] - '0';
else now = s[i] - 'a' + 10;
res = (res + now * (i ? 1 : 16));
}
return res;
}
long long helper(string A, string B) {
long long a = b10(A), b = b10(B);
return (a - b) * (a - b);
}
public:
string similarRGB(string color) {
long long mi = -1e18;
string A = color.substr(1,2), B = color.substr(3,2), C = color.substr(5,2);
string res = "";
vector<char> b16{'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
for(int i = 0; i < 16; i++) {
for(int j = 0; j < 16; j++) {
for(int k = 0; k < 16; k++) {
long long diff = -helper(A, string(2,b16[i])) -
helper(B, string(2,b16[j])) -
helper(C, string(2,b16[k]));

if(diff > mi) {
res = "#" + string(2,b16[i]) + string(2,b16[j]) + string(2,b16[k]);
mi = diff;
}
}
}
}


return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/15/PS/LeetCode/similar-rgb-color/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.