Hulu 9.16 笔试 第一题 100%
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class Main { private static int min = Integer.MAX_VALUE; private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); public static void main(String[] args) throws ParseException { Scanner scanner = new Scanner(System.in); int k = scanner.nextInt(); scanner.nextLine(); HashMap<String, ArrayList<String>> map = new HashMap<>(); for (int i = 0; i < k; i++) { String s = scanner.nextLine(); String[] split = s.split(" "); ArrayList<String> val = map.get(split[1]); if (val == null){ ArrayList<String> list = new ArrayList<>(); list.add(split[0]); map.put(split[1],list); }else { if (!val.contains(split[0])){ val.add(split[0]); map.put(split[1],val); } } } int n = scanner.nextInt(); ArrayList<Map.Entry<String, ArrayList<String>>> entries = new ArrayList<>(map.entrySet()); entries.sort(new Comparator<Map.Entry<String, ArrayList<String>>>() { @Override public int compare(Map.Entry<String, ArrayList<String>> o1, Map.Entry<String, ArrayList<String>> o2) { Date date1 = null; Date date2 = null; try { date1 = simpleDateFormat.parse(o1.getKey()); date2 = simpleDateFormat.parse(o2.getKey()); } catch (ParseException e) { throw new RuntimeException(e); } return date1.compareTo(date2); } }); for (int i = 0; i < entries.size(); i++) { count(entries,n,i); } System.out.println(min==Integer.MAX_VALUE?-1:min); } public static void count(ArrayList<Map.Entry<String, ArrayList<String>>> entries,int n,int index) throws ParseException { HashSet<String> set = new HashSet<>(); Date indexD = simpleDateFormat.parse(entries.get(index).getKey()); for (int i = index; i < entries.size(); i++) { Map.Entry<String, ArrayList<String>> entry = entries.get(i); set.addAll(entry.getValue()); if (set.size() >= n){ Date now = simpleDateFormat.parse(entries.get(i).getKey()); min = Math.min(min,(int)((now.getTime()-indexD.getTime())/1000/60/60/24)+1); return; } } } }
#hulu笔试#