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);
          }
    }
}

 

posted @ 2019-08-03 18:43  LeeJuly  阅读(111)  评论(0)    收藏  举报