modify code

master
algorithmzuo 1 year ago
parent 1cece71f21
commit 2d9b349407

@ -0,0 +1,48 @@
package 03.mca_06;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class Code01_Test1 {
// 题目描述这个题是关于数组求XXX东西
//
// 数组长度NN <= 10^5
// 100
// a b c ...
// 200
// a b c ...
// 100000
// a b c ..
public static int MAXN = 100010;
public static int[] arr = new int[MAXN];
public static int n;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StreamTokenizer in = new StreamTokenizer(br);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while (in.nextToken() != StreamTokenizer.TT_EOF) {
n = (int)in.nval;
for(int i = 0 ; i < n; i++) {
in.nextToken();
arr[i] = (int)in.nval;
}
// n = 1000
// arr 0..999
out.flush();
}
}
}

@ -0,0 +1,72 @@
package 03.mca_06;
public class Code01_Test2 {
public static class UnionFind {
// father[i] : i号点往上的是几号点
public int[] father;
// size[i] : i号点如果是代表点的话size[i]才有意义,代表集合的大小
public int[] size;
public int[] help;
public UnionFind(int n) {
father = new int[n];
size = new int[n];
help = new int[n];
for (int i = 0; i < n; i++) {
father[i] = i;
size[i] = 1;
}
}
// i号点往上到不能再往上是什么点返回
private int find(int i) {
int size = 0;
while (i != father[i]) {
help[size++] = i;
i = father[i];
}
while (size > 0) {
father[help[--size]] = i;
}
return i;
}
public boolean isSameSet(int x, int y) {
return find(x) == find(y);
}
// x背后的集合彻底和y背后的集合合在一起
public void union(int x, int y) {
int fx = find(x);
int fy = find(y);
if (fx != fy) {
int sizeX = size[fx];
int sizeY = size[fy];
if (sizeX >= sizeY) {
father[fy] = fx;
size[fx] += size[fy];
} else {
father[fx] = fy;
size[fy] += size[fx];
}
}
}
}
public static void main(String[] args) {
int n = 100;
UnionFind uf = new UnionFind(n);
System.out.println(uf.isSameSet(4, 60));
// 4 0 90
// 60 80 30
uf.union(4, 0);
uf.union(60, 80);
uf.union(0, 90);
uf.union(80, 30);
uf.union(90, 30);
System.out.println(uf.isSameSet(4, 60));
}
}

@ -6,14 +6,22 @@ package 第03期.mca_06;
// 返回 最少交换座位的次数,以便每对情侣可以并肩坐在一起
// 每次交换可选择任意两人,让他们站起来交换座位
// 测试链接 : https://leetcode.cn/problems/couples-holding-hands/
public class Code03_CouplesHoldingHands {
public class Code02_CouplesHoldingHands {
public int minSwapsCouples(int[] row) {
// 数字几个 2 * n 8 4 16 8
// 0 1 2 3 4 5 v
// 0 1 2 v/2
// n : 人数
// 情侣组 : n / 2
int n = row.length;
UnionFind uf = new UnionFind(n / 2);
for (int i = 0; i < n; i += 2) {
// 左 右 左 右
// 0 1 2 3 4
uf.union(row[i] / 2, row[i + 1] / 2);
}
// n / 2 -
return n / 2 - uf.sets();
}
@ -21,6 +29,7 @@ public class Code03_CouplesHoldingHands {
public int[] father;
public int[] size;
public int[] help;
// 当前有多少个集合?
public int sets;
public UnionFind(int n) {

@ -9,25 +9,27 @@ import java.util.HashMap;
// 其中 stones[i] = [xi, yi] 表示第 i 块石头的位置,
// 返回 可以移除的石子 的最大数量。
// 测试链接 : https://leetcode.cn/problems/most-stones-removed-with-same-row-or-column/
public class Code02_MostStonesRemovedWithSameRowOrColumn {
public class Code03_MostStonesRemovedWithSameRowOrColumn {
public static int removeStones(int[][] stones) {
int n = stones.length;
HashMap<Integer, Integer> rowPre = new HashMap<Integer, Integer>();
HashMap<Integer, Integer> colPre = new HashMap<Integer, Integer>();
// key : 某行 value : 首块石头的编号
HashMap<Integer, Integer> rowFirst = new HashMap<Integer, Integer>();
// key : 某列 value : 首块石头的编号
HashMap<Integer, Integer> colFirst = new HashMap<Integer, Integer>();
UnionFind uf = new UnionFind(n);
for (int i = 0; i < n; i++) {
int x = stones[i][0];
int y = stones[i][1];
if (!rowPre.containsKey(x)) {
rowPre.put(x, i);
if (!rowFirst.containsKey(x)) {
rowFirst.put(x, i);
} else {
uf.union(i, rowPre.get(x));
uf.union(i, rowFirst.get(x));
}
if (!colPre.containsKey(y)) {
colPre.put(y, i);
if (!colFirst.containsKey(y)) {
colFirst.put(y, i);
} else {
uf.union(i, colPre.get(y));
uf.union(i, colFirst.get(y));
}
}
return n - uf.sets();

@ -1,4 +1,4 @@
package 03.mca_06;
package 03.mca_07;
// 根据入度来求拓扑排序,牛客网的测试数据
// 测试链接 : https://www.nowcoder.com/questionTerminal/88f7e156ca7d43a1a535f619cd3f495c

@ -1,4 +1,4 @@
package 03.mca_06;
package 03.mca_07;
// 课上没讲这个实现
// 因为是一样的都是用Kruskal算法实现最小生成树只不过是牛客网的测试数据

@ -1,4 +1,4 @@
package 03.mca_06;
package 03.mca_07;
import java.util.ArrayList;
import java.util.PriorityQueue;
Loading…
Cancel
Save