migratoryBirds * output int arr element that

   /**
     * migratoryBirds
     * output int arr element that occus frequence is most high and rank at the lowest down layer
     * constriants:
     * arrList numberSize: 5<=n<=2*10to5
     * it is guaranteed that each arr element is 1,2,3,4,5
     * @param arr
     * @return
     */
    public static int migratoryBirds(List<Integer> arr){
        if (arr==null){
            return 0;
        }
        if (arr.isEmpty()){
            return 0;
        }
        int targetSize=1;
        for (int i = 0; i < 5; i++) {
            targetSize*=10;
        }
        if (arr.size()<5 || arr.size()>targetSize){
            return 0;
        }
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(1);
        integers.add(2);
        integers.add(3);
        integers.add(4);
        integers.add(5);
        for (Integer integer : arr) {
            if (!(integers.contains(integer))){
                return 0;
            }
        }
        HashMap<Integer, Integer> integerIntegerHashMap = new HashMap<>();
        for (Integer integer : arr) {
            if (integerIntegerHashMap.containsKey(integer)){
                integerIntegerHashMap.put(integer,integerIntegerHashMap.get(integer)+1);
            }else {
                integerIntegerHashMap.put(integer,1);
            }
        }
        ArrayList<CustomerIntStatistic> customerIntStatistics = new ArrayList<>();
        integerIntegerHashMap.forEach((k,v)->{
            CustomerIntStatistic customerIntStatistic = new CustomerIntStatistic();
            customerIntStatistic.setId(UUID.randomUUID().toString());
            customerIntStatistic.setArrIntEle(k);
            customerIntStatistic.setArrIntEleOccusFrequence(v);
            customerIntStatistics.add(customerIntStatistic);
        });
        Collections.sort(customerIntStatistics, new Comparator<CustomerIntStatistic>() {
            @Override
            public int compare(CustomerIntStatistic o1, CustomerIntStatistic o2) {
                if (o1.getArrIntEleOccusFrequence()>o2.getArrIntEleOccusFrequence()){
                    return 1;
                } else if (o1.getArrIntEleOccusFrequence()<o2.getArrIntEleOccusFrequence()) {
                    return -1;
                } else if (o1.getArrIntEleOccusFrequence()==o2.getArrIntEleOccusFrequence()) {
                    if (o1.getArrIntEle()<o2.getArrIntEle()){
                        return 1;
                    } else if (o1.getArrIntEle()>o2.getArrIntEle()) {
                        return -1;
                    }
                }
                return 0;
            }
        });
        return customerIntStatistics.get(customerIntStatistics.size()-1).getArrIntEle();
    }

class CustomerIntStatistic{
    private String id;
    private Integer arrIntEle;
    private Integer arrIntEleOccusFrequence;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Integer getArrIntEle() {
        return arrIntEle;
    }

    public void setArrIntEle(Integer arrIntEle) {
        this.arrIntEle = arrIntEle;
    }

    public Integer getArrIntEleOccusFrequence() {
        return arrIntEleOccusFrequence;
    }

    public void setArrIntEleOccusFrequence(Integer arrIntEleOccusFrequence) {
        this.arrIntEleOccusFrequence = arrIntEleOccusFrequence;
    }

    @Override
    public String toString() {
        return "CustomerIntStatistic{" +
                "id='" + id + '\'' +
                ", arrIntEle=" + arrIntEle +
                ", arrIntEleOccusFrequence=" + arrIntEleOccusFrequence +
                '}';
    }
}

#牛客AI配图神器#

#牛客创作赏金赛##如何缓解求职过程中的焦虑?#
Java技术 文章被收录于专栏

JavaEE技术 编程开发经验 企业通用技术

全部评论

相关推荐

07-10 11:08
门头沟学院 Java
Sairus:我注册都注册不了提醒我手机号二次啥的,果然对于人才推得就是快,像我投完了就没回音的
投递京东等公司9个岗位
点赞 评论 收藏
分享
自学java狠狠赚一...:骗你点star的,港卵公司,记得把star收回去
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务