One of my hobbies is solving algorithm problem in topcoder.
I'm very interested in learning.
Today I solve this problem.
http://community.topcoder.com/stat?c=problem_statement&pm=13543&rd=16082
My access was make two subsets and there are intersection between A and B
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package srm639; import java.util.HashSet; /** * 14분 걸림 * @author ddavid * */ public class ElectronicPetEasy { public static void main(String[] args) { // TODO Auto-generated method stub ElectronicPetEasy ep= new ElectronicPetEasy(); // System.out.println(ep.isDifficult(3, 3, 3, 5, 2, 3)); // System.out.println(ep.isDifficult(1, 1000, 1000, 2, 1000, 1000)); System.out.println(ep.isDifficult(1, 1, 1, 2, 2, 2)); } public String isDifficult(int st1, int p1, int t1, int st2, int p2, int t2){ HashSet<Integer> set = new HashSet<Integer>(); HashSet<Integer> set2= new HashSet<Integer>(); set.add(st1); for(int i=1; i<t1; i++){ int val = st1+(p1*i); set.add(val); } set2.add(st2); for(int i=1; i<t2; i++){ int val = st2+(p2*i); set2.add(val); } System.out.println(set); System.out.println(set2); int totalSize =set.size() + set2.size(); //merge size set.addAll(set2); if(totalSize == set.size()){ return "Easy"; }else{ return "Difficult"; } } } |
No comments:
Post a Comment