forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_128.java
166 lines (149 loc) · 5.13 KB
/
_128.java
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
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 {
/**
* O(n) time complexity.
*/
public int longestConsecutive(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}
int longestStreak = 0;
for (int num : set) {
//we'll go through this set instead of nums, this makes a big difference in time complexity, esp. based on LeetCode test cases
if (!set.contains(num - 1)) {
int currentNum = num;
int currentStreak = 1;
while (set.contains(currentNum + 1)) {
currentNum += 1;
currentStreak += 1;
}
longestStreak = Math.max(longestStreak, currentStreak);
}
}
return longestStreak;
}
}
public static class Solution4 {
/**
* O(nlogn) time complexity
*/
public int longestConsecutive(int[] nums) {
if (nums.length == 0) {
return 0;
}
TreeSet<Integer> treeSet = new TreeSet<>();
for (int i : nums) {
treeSet.add(i);//O(logn) time complexity for each add() call
}
int ans = 1;
Iterator<Integer> it = treeSet.iterator();
Integer curr = it.next();
int len = 1;
while (it.hasNext()) {
Integer next = it.next();
if (curr + 1 == next) {
len++;
} else {
len = 1;
}
curr = next;
ans = Math.max(ans, len);
}
ans = Math.max(ans, len);
return ans;
}
}
}