Created
May 29, 2011 21:35
-
-
Save stphung/998155 to your computer and use it in GitHub Desktop.
TopCoder SRM 507 Division 2 - 500 point problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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