Skip to content

Instantly share code, notes, and snippets.

@stphung
Created May 29, 2011 21:35
Show Gist options
  • Select an option

  • Save stphung/998155 to your computer and use it in GitHub Desktop.

Select an option

Save stphung/998155 to your computer and use it in GitHub Desktop.
TopCoder SRM 507 Division 2 - 500 point problem
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class CubeStickers {
public String isPossible(String[] sticker) {
Set<String> uniqueStickers = new HashSet<String>();
for (String s : sticker) uniqueStickers.add(s);
if (uniqueStickers.size() >= 5) return "YES";
else {
Map<String, Integer> colorsMap = new HashMap<String, Integer>();
for (String s : sticker) colorsMap.put(s,colorsMap.containsKey(s) ? colorsMap.get(s) + 1 : 1);
int pairs = 0;
int singles = 0;
for (Map.Entry<String, Integer> entry : colorsMap.entrySet()) {
if (entry.getValue() >= 2) pairs++;
else singles++;
}
return pairs * 2 + singles >= 6 ? "YES" : "NO";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment