Skip to content

Commit 4c79859

Browse files
add 800
1 parent ae3d958 commit 4c79859

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Your ideas/fixes/algorithms are more than welcome!
2525
|811|[Subdomain Visit Count](https://leetcode.com/problems/subdomain-visit-count/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_811.java) | O(n) | O(n) | |Easy| HashMap
2626
|806|[Number of Lines To Write String](https://leetcode.com/problems/number-of-lines-to-write-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_806.java) | O(n) | O(1) | |Easy|
2727
|804|[Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_804.java) | O(S) | O(S) | |Easy|
28+
|800|[Similar RGB Color](https://leetcode.com/problems/similar-rgb-color/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_800.java) | O(1) | O(1) | |Easy|
2829
|799|[Champagne Tower](https://leetcode.com/problems/champagne-tower/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_799.java) | O(r^2) or O(1) | O(r^2) or O(1) | |Medium|
2930
|796|[Rotate String](https://leetcode.com/problems/rotate-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_796.java) | O(n) | O(1) | |Easy|
3031
|791|[Custom Sort String](https://leetcode.com/problems/custom-sort-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_791.java) | O(n+m) | O(1) | |Medium|
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* 800. Similar RGB Color
9+
10+
In the following, every capital letter represents some hexadecimal digit from 0 to f.
11+
12+
The red-green-blue color "#AABBCC" can be written as "#ABC" in shorthand. For example, "#15c" is shorthand for the color "#1155cc".
13+
14+
Now, say the similarity between two colors "#ABCDEF" and "#UVWXYZ" is -(AB - UV)^2 - (CD - WX)^2 - (EF - YZ)^2.
15+
16+
Given the color "#ABCDEF", return a 7 character color that is most similar to #ABCDEF, and has a shorthand (that is, it can be represented as some "#XYZ"
17+
18+
Example 1:
19+
Input: color = "#09f166"
20+
Output: "#11ee66"
21+
Explanation:
22+
The similarity is -(0x09 - 0x11)^2 -(0xf1 - 0xee)^2 - (0x66 - 0x66)^2 = -64 -9 -0 = -73.
23+
This is the highest among any shorthand color.
24+
25+
Note:
26+
27+
color is a string of length 7.
28+
color is a valid RGB color: for i > 0, color[i] is a hexadecimal digit from 0 to f
29+
Any answer which has the same (highest) similarity as the best answer will be accepted.
30+
All inputs and outputs should use lowercase letters, and the output is 7 characters.
31+
32+
*/
33+
public class _800 {
34+
public static class Solution1 {
35+
public String similarRGB(String color) {
36+
List<String> allShortHandCombinations = computeAllShorthandCombinations();
37+
int minSimilarity = Integer.MIN_VALUE;
38+
String result = "";
39+
for (String candidate : allShortHandCombinations) {
40+
int similarity = computeSimilarity(candidate, color);
41+
if (similarity > minSimilarity) {
42+
result = candidate;
43+
minSimilarity = similarity;
44+
}
45+
}
46+
return result;
47+
}
48+
49+
private int computeSimilarity(String candidate, String color) {
50+
return -(Integer.parseInt(candidate.substring(1, 3), 16) - Integer.parseInt(
51+
color.substring(1, 3), 16)) * (Integer.parseInt(candidate.substring(1, 3), 16)
52+
- Integer.parseInt(color.substring(1, 3), 16))
53+
- (Integer.parseInt(candidate.substring(3, 5), 16) - Integer.parseInt(
54+
color.substring(3, 5), 16)) * (Integer.parseInt(candidate.substring(3, 5), 16)
55+
- Integer.parseInt(color.substring(3, 5), 16))
56+
- (Integer.parseInt(candidate.substring(5, 7), 16) - Integer.parseInt(
57+
color.substring(5, 7), 16)) * (Integer.parseInt(candidate.substring(5, 7), 16)
58+
- Integer.parseInt(color.substring(5, 7), 16));
59+
}
60+
61+
private List<String> computeAllShorthandCombinations() {
62+
List<String> result = new ArrayList<>();
63+
List<Character> hexNumber = new ArrayList<>(
64+
Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
65+
'f'));
66+
for (int i = 0; i < hexNumber.size(); i++) {
67+
for (int j = 0; j < hexNumber.size(); j++) {
68+
for (int k = 0; k < hexNumber.size(); k++) {
69+
StringBuilder sb = new StringBuilder();
70+
sb.append("#");
71+
sb.append(hexNumber.get(i));
72+
sb.append(hexNumber.get(i));
73+
sb.append(hexNumber.get(j));
74+
sb.append(hexNumber.get(j));
75+
sb.append(hexNumber.get(k));
76+
sb.append(hexNumber.get(k));
77+
result.add(sb.toString());
78+
}
79+
}
80+
}
81+
return result;
82+
}
83+
}
84+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._800;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _800Test {
10+
private static _800.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _800.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("#11ee66", solution1.similarRGB("#09f166"));
20+
}
21+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy