Skip to content

Commit 3c47c51

Browse files
author
rchen102
committed
Union Find
1 parent 4eed192 commit 3c47c51

File tree

8 files changed

+433
-258
lines changed

8 files changed

+433
-258
lines changed

JZOffer/JZ 60.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public double[] twoSum(int n) {
3+
if (n == 0) return new double[0];
4+
int max_value = 6;
5+
int len = max_value * n + 1;
6+
int[][] dp = new int[n + 1][len];
7+
// base case
8+
for (int j = 1; j <= max_value; j++) {
9+
dp[1][j] = 1;
10+
}
11+
// dp 推导
12+
for (int i = 2; i <= n; i++) {
13+
for (int j = 1; j <= i * max_value; j++) {
14+
for (int v = 1; v <= max_value; v++) {
15+
if (j >= v) {
16+
dp[i][j] += dp[i-1][j-v];
17+
}
18+
}
19+
}
20+
}
21+
int counter = max_value * n - n + 1;
22+
int startVal = n;
23+
double total = Math.pow(max_value, n);
24+
double[] res = new double[counter];
25+
for (int i = 0; i < counter; i++) {
26+
int val = startVal + i;
27+
res[i] = (double)dp[n][val] / total;
28+
}
29+
return res;
30+
}
31+
}

JZOffer/jz 45.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public String minNumber(int[] nums) {
3+
List<String> list = new ArrayList<>();
4+
for (int n : nums) {
5+
list.add(String.valueOf(n));
6+
}
7+
Collections.sort(list, (a, b) -> (a + b).compareTo(b + a));
8+
StringBuilder res = new StringBuilder();
9+
for (String str : list) {
10+
res.append(str);
11+
}
12+
return res.toString();
13+
}
14+
}

Java/leetcode 128.java

Lines changed: 82 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,107 @@
1-
//Solution1: HashMap T: O(n) S: O(n)
1+
// Solution1: HashSet T: O(n) S: O(n)
22
class Solution {
33
public int longestConsecutive(int[] nums) {
4+
if (nums == null || nums.length == 0) return 0;
5+
Set<Integer> set = new HashSet<>();
6+
// 去重
7+
for (int n : nums) {
8+
set.add(n);
9+
}
410
int res = 0;
5-
HashMap<Integer, Integer> map = new HashMap<>();
6-
for(int n : nums) {
7-
if(!map.containsKey(n)) {
8-
int left = (map.containsKey(n-1)) ? map.get(n-1) : 0;
9-
int right = (map.containsKey(n+1)) ? map.get(n+1) : 0;
10-
int sum = left + right + 1;
11-
map.put(n, sum);
12-
13-
res = Math.max(res, sum);
14-
15-
map.put(n-left, sum);
16-
map.put(n+right, sum);
11+
for (int n : nums) {
12+
if (set.contains(n-1)) continue;
13+
int tmpLen = 1;
14+
int cur = n + 1;
15+
while (set.contains(cur)) {
16+
tmpLen++;
17+
cur++;
1718
}
19+
res = Math.max(res, tmpLen);
1820
}
1921
return res;
2022
}
2123
}
2224

23-
//Solution2: HashSet T: O(n) S: O(n)
24-
class Solution {
25-
public int longestConsecutive(int[] nums) {
26-
Set<Integer> set = new HashSet<>();
27-
for(int n : nums) {
28-
set.add(n);
25+
// Solution2: union find 近似 T: O(n) S: O(n)
26+
class UnionFind {
27+
int[] parent;
28+
int[] rank;
29+
int maxRank;
30+
31+
UnionFind(int n) {
32+
parent = new int[n];
33+
rank = new int[n];
34+
for (int i = 0; i < n; i++) {
35+
parent[i] = i;
36+
rank[i] = 1;
2937
}
30-
31-
int res = 0;
32-
for(int n : set) {
33-
if(!set.contains(n-1)) {
34-
int m = n + 1;
35-
while(set.contains(m)) {
36-
m++;
37-
}
38-
res = Math.max(res, m - n);
39-
}
38+
maxRank = 1;
39+
}
40+
41+
int find(int x) {
42+
while (x != parent[x]) {
43+
parent[x] = parent[parent[x]];
44+
x = parent[x];
45+
}
46+
return x;
47+
}
48+
49+
void union(int p, int q) {
50+
int rootp = find(p);
51+
int rootq = find(q);
52+
if (rootp == rootq) return;
53+
if (rank[rootp] > rank[rootq]) {
54+
parent[rootq] = rootp;
55+
rank[rootp] += rank[rootq];
56+
maxRank = Math.max(maxRank, rank[rootp]);
57+
}
58+
else {
59+
parent[rootp] = rootq;
60+
rank[rootq] += rank[rootp];
61+
maxRank = Math.max(maxRank, rank[rootq]);
4062
}
41-
return res;
4263
}
4364
}
4465

45-
//Solution3: union find T: O(n) S: O(n)
4666
class Solution {
4767
public int longestConsecutive(int[] nums) {
48-
int length = nums.length;
49-
UnionFind uf = new UnionFind(length);
50-
HashMap<Integer, Integer> map = new HashMap<>();
51-
for(int i = 0; i < length; i++) {
52-
if(!map.containsKey(nums[i])) {
53-
map.put(nums[i], i);
54-
if(map.containsKey(nums[i]-1)) {
55-
uf.union(i, map.get(nums[i]-1));
56-
}
57-
if(map.containsKey(nums[i]+1)) {
58-
uf.union(i, map.get(nums[i]+1));
59-
}
68+
if (nums == null || nums.length == 0) return 0;
69+
Map<Integer, Integer> map = new HashMap<>();
70+
UnionFind uf = new UnionFind(nums.length);
71+
// 建立映射,去重
72+
for (int i = 0; i < nums.length; i++) {
73+
if (!map.containsKey(nums[i])) map.put(nums[i], i);
74+
}
75+
for (int n : nums) {
76+
if (map.containsKey(n+1)) {
77+
uf.union(map.get(n), map.get(n+1));
6078
}
6179
}
62-
return uf.maxUnion();
63-
80+
return uf.maxRank;
6481
}
6582
}
6683

67-
class UnionFind {
68-
int[] father;
69-
70-
UnionFind(int length) {
71-
father = new int[length];
72-
for(int i = 0; i < length; i++) {
73-
father[i] = i;
74-
}
75-
}
76-
77-
public int maxUnion() {
78-
int[] count = new int[father.length];
84+
85+
86+
//Solution: 不太好理解 HashMap T: O(n) S: O(n)
87+
class Solution {
88+
public int longestConsecutive(int[] nums) {
7989
int res = 0;
80-
for(int i = 0; i < father.length; i++) {
81-
count[find(i)]++;
82-
res = Math.max(res, count[father[i]]);
90+
HashMap<Integer, Integer> map = new HashMap<>();
91+
for(int n : nums) {
92+
if(!map.containsKey(n)) {
93+
int left = (map.containsKey(n-1)) ? map.get(n-1) : 0;
94+
int right = (map.containsKey(n+1)) ? map.get(n+1) : 0;
95+
int sum = left + right + 1;
96+
map.put(n, sum);
97+
98+
res = Math.max(res, sum);
99+
100+
map.put(n-left, sum);
101+
map.put(n+right, sum);
102+
}
83103
}
84104
return res;
85105
}
86-
87-
public int find(int node) {
88-
if(node == father[node]) return father[node];
89-
father[node] = find(father[node]);
90-
return father[node];
91-
}
92-
93-
public void union(int node1, int node2) {
94-
int find1 = find(node1);
95-
int find2 = find(node2);
96-
if(find1 != find2) {
97-
father[find2] = find1;
98-
}
99-
}
100-
}
106+
}
107+

0 commit comments

Comments
 (0)