2019.8.3网易


这里不能排序加二分查找,时间复杂度太高
要用到hashmap,但有一些问题,就是关于key的插入,所以这里需要用我们自己的对象
public class Solution { static class ss extends Object{ int value; ss(int value){ this.value=value; } public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof ss)) { return false; } ss s = (ss) obj; return this.value == s.value; } @Override public int hashCode() { return value; } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int k=sc.nextInt(); ConcurrentHashMap<ss, Integer> map=new ConcurrentHashMap<>(); for(int i=0;i<n;i++) { ss a=new ss(sc.nextInt()); if(map.containsKey(a)){ map.put(a, map.get(a)+1); } else { map.put(a, 1); } } for(int j=0;j<k;j++) { int z=sc.nextInt(); int res=0; Iterator<ss> iter = map.keySet().iterator(); while(iter.hasNext()) { ss i=iter.next(); ss m=new ss(i.value-1); if(i.value>=z) { res+=map.get(i); map.put(m, map.getOrDefault(m, 0)+map.get(i)); map.put(i,0); } } System.out.println(res); } } }
本文来自博客园,作者:LeeJuly,转载请注明原文链接:https://www.cnblogs.com/peterleee/p/11295804.html

浙公网安备 33010602011771号