publicclasst1{publicstaticintsolution(int[] A){// write your code in Java SE 8int 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;}publicstaticvoidmain(String[] args){int[] A =newint[]{-1,-2,-3};
System.out.println(solution(A));}}
publicclasst2{// 注意去重的情况publicstaticintsolution(int N,int[] A,int[] B){// write your code in Java SE 8// 存的是次数int[] jilu =newint[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(newInteger[0]);
Arrays.sort(jisuan, Collections.reverseOrder());int i =0;while(jisuan[i]>0){
sum += jisuan[i]* N;
N--;
i++;}return sum;}publicstaticvoidmain(String[] args){int N =3;int[] A =newint[]{1};int[] B =newint[]{3};
System.out.println(solution(N, A, B));}}
publicclasst3{publicstaticintsolution(int[] A,int M){// write your code in Java SE 8
UnionFind unionFind =newUnionFind(A.length);boolean[] mark =newboolean[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 =newHashMap<>();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;}// 并查集staticclassUnionFind{privateint[] parent;privateint[] rank;UnionFind(int n){this.parent =newint[n];for(int i =0; i < n;++i){
parent[i]= i;}this.rank =newint[n];}intfind(int idx){while(parent[idx]!= idx) idx = parent[idx];return idx;}voidunion(int x,int y){int xRoot =find(x);int yRoot =find(y);if(xRoot == yRoot)return;if(rank[xRoot]> rank[yRoot]){
parent[yRoot]= xRoot;}elseif(rank[xRoot]< rank[yRoot]){
parent[xRoot]= yRoot;}else{
parent[yRoot]= xRoot;
rank[xRoot]++;}}}// 建立数学模型 简化思路publicstaticintsolution1(int[] A,int M){int result =0;
HashMap<Integer, Integer> set =newHashMap<>();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;}publicstaticvoidmain(String[] args){int[] A =newint[]{-3,-2,1,0,8,7,1};
System.out.println(solution(A,8));
System.out.println(solution1(A,8));}}