forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_128.java
More file actions
129 lines (113 loc) · 4 KB
/
_128.java
File metadata and controls
129 lines (113 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class _128 {
public static class Solution1 {
public int longestConsecutive(int[] nums) {
Map<Integer, Integer> map = new HashMap();
//<value, index>
UnionFind uf = new UnionFind(nums);
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
continue;
}
map.put(nums[i], i);
if (map.containsKey(nums[i] - 1)) {
uf.union(i, map.get(nums[i] - 1));
//note: we want to union this index and nums[i]-1's root index which we can get from the map
}
if (map.containsKey(nums[i] + 1)) {
uf.union(i, map.get(nums[i] + 1));
}
}
return uf.maxUnion();
}
class UnionFind {
int[] ids;
public UnionFind(int[] nums) {
ids = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
ids[i] = i;
}
}
public void union(int i, int j) {
int x = find(ids, i);
int y = find(ids, j);
ids[x] = y;
}
public int find(int[] ids, int i) {
while (i != ids[i]) {
ids[i] = ids[ids[i]];
i = ids[i];
}
return i;
}
public boolean connected(int i, int j) {
return find(ids, i) == find(ids, j);
}
public int maxUnion() {
//this is O(n)
int max = 0;
int[] count = new int[ids.length];
for (int i = 0; i < ids.length; i++) {
count[find(ids, i)]++;
max = max < count[find(ids, i)] ? count[find(ids, i)] : max;
}
return max;
}
}
}
public static class Solution2 {
//inspired by this solution: https://discuss.leetcode.com/topic/25493/simple-fast-java-solution-using-set
public int longestConsecutive(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
Set<Integer> set = new HashSet();
for (int i : nums) {
set.add(i);
}
int max = 1;
for (int num : nums) {
if (set.remove(num)) {
int val = num;
int count = 1;
while (set.remove(val - 1)) {
val--;//we find all numbers that are smaller than num and remove them from the set
}
count += num - val;
val = num;
while (set.remove(val + 1)) {
val++;//then we find all numbers that are bigger than num and also remove them from the set
}
count += val - num;
max = Math.max(max, count);
}
}
return max;
}
}
public static class Solution3 {
public int longestConsecutive(int[] nums) {
HashSet<Integer> numSet = new HashSet<>();
for (int num : nums) {
numSet.add(num);
}
int longestStreak = 0;
for (int num : nums) {
if (!numSet.contains(num - 1)) {
int currentNum = num;
int currentStreak = 1;
while (numSet.contains(currentNum + 1)) {
currentNum += 1;
currentStreak += 1;
}
longestStreak = Math.max(longestStreak, currentStreak);
}
}
return longestStreak;
}
}
}