微软第三次笔试 3.28

在这里插入图片描述

在这里插入图片描述

public class t1 {
    public static int solution(int[] A) {
        // write your code in Java SE 8
        int result = 0;

        int sum = 0;
        for (int i : A) {
            if (i < 0) {
                result = Math.max(result, sum);
                sum = 0;
            } else {
                sum += i;
            }
        }

        result = Math.max(result, sum);
        return result;
    }

    public static void main(String[] args) {
        int[] A = new int[]{-1, -2, -3};
        System.out.println(solution(A));
    }
}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

public class t2 {

    //    注意去重的情况
    public static int solution(int N, int[] A, int[] B) {
        // write your code in Java SE 8
//        存的是次数
        int[] jilu = new int[N + 1];
        int sum = 0;
        for (int i = 0; i < A.length; i++) {
            jilu[A[i]]++;
            jilu[B[i]]++;
        }

        Integer[] jisuan = IntStream.of(jilu).boxed().collect(Collectors.toList()).toArray(new Integer[0]);

        Arrays.sort(jisuan, Collections.reverseOrder());

        int i = 0;
        while (jisuan[i] > 0) {
            sum += jisuan[i] * N;
            N--;
            i++;
        }

        return sum;
    }

    public static void main(String[] args) {
        int N = 3;
        int[] A = new int[]{1};
        int[] B = new int[]{3};
        System.out.println(solution(N, A, B));

    }

}

在这里插入图片描述

public class t3 {
    public static int solution(int[] A, int M) {
        // write your code in Java SE 8
        UnionFind unionFind = new UnionFind(A.length);
        boolean[] mark = new boolean[A.length];
        for (int i = 0; i < A.length; ++i) {
            if (mark[i]) continue;
            for (int j = 0; j < A.length; ++j) {
                if (i == j) continue;
                if (Math.abs(A[i] - A[j]) % M == 0) {
                    unionFind.union(i, j);// 如果符合条件,就进行合并
                    mark[j] = true;// 标记合并过的point,减少循环次数
                }
            }
        }
        HashMap<Integer, Integer> map = new HashMap<>();
        int res = 0;
        for (int item : unionFind.parent) {
            int curNum = map.getOrDefault(item, 0);
            map.put(item, curNum + 1);
            res = Math.max(res, curNum + 1);
        }
        return res;
    }

    //    并查集
    static class UnionFind {
        private int[] parent;
        private int[] rank;

        UnionFind(int n) {
            this.parent = new int[n];
            for (int i = 0; i < n; ++i) {
                parent[i] = i;
            }
            this.rank = new int[n];
        }

        int find(int idx) {
            while (parent[idx] != idx) idx = parent[idx];
            return idx;
        }

        void union(int x, int y) {
            int xRoot = find(x);
            int yRoot = find(y);
            if (xRoot == yRoot) return;
            if (rank[xRoot] > rank[yRoot]) {
                parent[yRoot] = xRoot;
            } else if (rank[xRoot] < rank[yRoot]) {
                parent[xRoot] = yRoot;
            } else {
                parent[yRoot] = xRoot;
                rank[xRoot]++;
            }
        }
    }

    //    建立数学模型 简化思路
    public static int solution1(int[] A, int M) {
        int result = 0;
        HashMap<Integer, Integer> set = new HashMap<>();
        for (int i : A) {
            set.put(i % M, set.getOrDefault(i % M, 0) + 1);
        }

        for (int i = 0; i < set.size(); i++) {
            result = Math.max(result, set.getOrDefault(i, 0));
        }

        return result;
    }

    public static void main(String[] args) {

        int[] A = new int[]{-3, -2, 1, 0, 8, 7, 1};
        System.out.println(solution(A, 8));
        System.out.println(solution1(A, 8));

    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值